为什么我的 Vue 实例的属性和方法在模板中不可用? [英] Why are my Vue instance's properties and methods not available in the template?
问题描述
- 我建立了一个 Vue.js CLI 项目.
- 在页面上,我想在
data()
中定义一个变量,然后在我的模板中使用它. - 为什么下面的代码告诉我:
属性或方法消息"未在实例上定义,但渲染时引用.确保声明反应性数据数据选项中的属性.
<div><h1>第2页</h1><p>你可以回到<router-link to="/">home</router-link>.</p><p>[{{message}}]</p>
</模板>导出默认{名称:第2页",数据() {返回 {消息:在这里"}}}
<小时>还有这段代码:
<button class="btn" @click="test()">测试</button></p>
</模板>导出默认{名称:第2页",数据() {返回 {消息:在这里"}},方法: {测试() {console.log('test');}}}
类似地告诉我:
<块引用>属性或方法test"未在实例上定义,但渲染时引用.确保声明反应性数据数据选项中的属性.
我还需要做什么才能使这些变量和方法在页面上的模板中可用?
这种代码结构在其他 Vue.js CLI 项目中也有效,因此在项目环境中的某处设置不正确.
例如,此代码适用于另一个项目:
<div class="start alert alert-success" role="alert">{{消息}}
</模板><脚本>导出默认{name: '开始',数据() {返回 {msg: '请选择一个选项.'}}}
它适用于 Home
页面,但不适用于 Page1
或 Page2
.我的 index.js
页面如下所示:
从vue"导入Vue;从vue-router"导入路由器;从@/components/Home"导入主页;从@/components/Page1"导入Page1;从@/components/Page2"导入Page2;Vue.use(路由器);导出默认的新路由器({路线:[{小路: "/",name: "家",组件:首页},{路径:/page1",名称:第1页",组件:第1页},{路径:/page2",名称:第2页",组件:第2页}]});
您需要在 Page1 和 Page2 组件的 javascript 周围添加 标签.
否则,似乎 vue-loader
会忽略该脚本并且不会给您相关警告(只是模板中引用的数据丢失).
- I set up a Vue.js CLI project.
- On a page, I want to define a variable in
data()
and then use it in my template. - Why does the following code tell me:
Property or method "message" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.
<template>
<div>
<h1>Page 2</h1>
<p> You can go back to
<router-link to="/">home</router-link>.</p>
<p>[{{message}}]</p>
</div>
</template>
export default {
name: "Page2",
data() {
return {
message: "here it is"
}
}
}
And this code:
<button class="btn" @click="test()">the test</button>
</p>
</div>
</template>
export default {
name: "Page2",
data() {
return {
message: "here it is"
}
},
methods: {
test() {
console.log('test');
}
}
}
Tells me similarly:
Property or method "test" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.
What else do I need to do to make these variables and methods available in my template on the page?
This structure of code has worked in other Vue.js CLI projects so it much be something not set right in the project in the environment somewhere.
For instance, this code works in another project:
<template>
<div class="start alert alert-success" role="alert">
{{ msg }}
</div>
</template>
<script>
export default {
name: 'start',
data() {
return {
msg: 'Please choose an option.'
}
}
}
</script>
And it works on the Home
page but not on Page1
or Page2
. My index.js
page looks like this:
import Vue from "vue";
import Router from "vue-router";
import Home from "@/components/Home";
import Page1 from "@/components/Page1";
import Page2 from "@/components/Page2";
Vue.use(Router);
export default new Router({
routes: [
{
path: "/",
name: "Home",
component: Home
},
{
path: "/page1",
name: "Page1",
component: Page1
},
{
path: "/page2",
name: "Page2",
component: Page2
}
]
});
You need to add <script>
tags around the javascript for your Page1 and Page2 components.
Otherwise, it seems like vue-loader
just ignores that script and doesn't give you a relevant warning (just that the data being referenced in your template is missing).
这篇关于为什么我的 Vue 实例的属性和方法在模板中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!