Vue学习之内联模板
在vue框架中,模板的位置有两种,一种是在组件内部定义,一种是在组件外部定义。
我们首先来看内部的定义,即常见的定义方式:
Vue.component('child-component',{
template:'<div><p>Welcome to Vue!</p></div>',
data:function(){
return {
msg:'在子组件中声明的数据'
}
}
});
在这里,我们的模板是在创建一个全局组件时定义在template中的,我们为你来看一下整个代码:
<html>
<head>
<meta charset="utf-8">
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
<p>{
{
message}}</p>
<child-component>
</child-component>
</div>
<script>
Vue.component('child-component',{
template:'<div><p>Welcome to Vue!</p></div>'
});