我想知道如何从外部更改 vue 应用程序的属性。所以我想创建一个名为 app 的新 vue 应用程序并设置 app.propertyName = someValue,但正如下面尝试的那样,这不起作用。你如何实现这一目标?
<!DOCTYPE html>
<html>
<head>
<title>The template configuration option</title>
<style>
#app {
display: inline-block;
padding: 10px;
font-size: x-large;
background-color: lightgreen;
}
</style>
</head>
<body>
<h1>'template' Example</h1>
<p>All HTML code from inside the div tag with id="app" is moved inside the 'template' configuration option, in backtick quotes.</p>
<div id="app"></div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
template: `<h1>{{ message }}</h1>
<p>This is a second line of HTML code, inside backtick quotes</p>`,
data() {
return {
message: "Hello World!"
}
}
})
app.mount('#app');
app.message = "My name is Florian"; //This line of code doesn't work
</script>
</body>
</html>
最佳答案
您可以使用 $data 访问或编辑消息,例如:
app.$data.message = "My name is Florian"; // Access the reactive data object using $data
关于javascript - 如何从外部更改vue应用程序的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76145775/