<body>
<div id="app">
<h3>{{firstName + ' '+ lastName}}</h3>
<h3>{{firstName}} {{lastName}}</h3>
<h3>{{getFullName()}}</h3>
<h3>{{fullName}}</h3>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
firstName: "kobe",
lastName: "bryant"
},
computed: {
fullName: function() {
return this.firstName+' '+this.lastName
}
},
methods: {
getFullName() {
return this.firstName+' '+this.lastName
}
}
})
</script>
</body>
测试正常,可以显示,请检查vue.js地址是否引用错误,另外vue是不支持ie8及以下版本。
测试截图
附上我的测试代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<div id="app">
<h3>{{firstName + ' '+ lastName}}</h3>
<h3>{{firstName}} {{lastName}}</h3>
<h3>{{getFullName()}}</h3>
<h3>{{fullName}}</h3>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
firstName: "kobe",
lastName: "bryant"
},
computed: {
fullName: function() {
return this.firstName+' '+this.lastName
}
},
methods: {
getFullName() {
return this.firstName+' '+this.lastName
}
}
})
</script>
</body>
</html>
代码测试没问题,看看vue.js是否正确引入了。
如果用的是ie浏览器,不支持对象中简式定义方法
getFullName() {
return this.firstName+' '+this.lastName
}
要改成
getFullName:function() {
return this.firstName+' '+this.lastName
}