8000
vue组件在这里是可扩展的html自定义元素,封装可复用的代码。比如
<div id="example"> <my-component></my-component> </div>
var MyComponent = Vue.extend({ // 选项... }) // 全局注册组件,tag 为 my-component Vue.component('my-component', MyComponent)
var Child = Vue.extend({ /* ... */ }) var Parent = Vue.extend({ template: '...', components: { // <my-component> 只能用在父组件模板内 'my-component': Child } })
组件实例的作用域是孤立的。这意味着不能并且不应该在子组件的模板内直接引用父组件的数据。可以使用 props 把数据传给子组件。
Vue.component('child', { // 声明 props props: ['msg'], // prop 可以用在模板内 // 可以用 `this.msg` 设置 template: '<span>{{ msg }}</span>' })
<child msg="hello!"></child>
<child v-bind:my-message="parentMsg"></child>
例子:
<app> <app-header></app-header> <app-footer></app-footer> </app>
疑问:如果app下也有自己的模板,怎么处理。
<my-component> <p>This is some original content</p> <p>This is some more original content</p> </my-component>
<div> <h1>This is my component!</h1> <slot> 如果没有分发内容则显示我。 </slot> </div>
<div> <h1>This is my component!</h1> <p>This is some original content</p> <p>This is some more original content</p> </div>
<div> <h1>This is my component!</h1> </div>
==== 以上就是个人觉得初识vue需要了解到的vue 组件相关的一些细节,其实还有很多详细的内容,都欢迎到官网进行查看吧。
下一篇计划是介绍一下vue-router并结合vue-router来做一个spa webapp,敬请期待咯
Activity