<button>abc</button>
</div>
最后统统不见,被替换成<div>A cu stom component!</div>。灵活度太低,如果我只想替换ssssdfsdaf怎么办?所以就要用第一种方式了,于是幡然醒悟,原来这就是组件,就像一个零件一样,想往哪塞就往哪塞:
<div id="example">
<my-template>ssssdfsdaf<my-template>
<button>abc</button>
</div>另外,注册必须在新建实例前,反过来的话,新建的实例肯定不能使用组件的。
原文还说replace可以决定是否替换,这个不知道咋用,先留一坑在这,后面看看能否用上。 //坑1
2.组件注册有两种方式:
一是前面看到的全局注册方式,Vue.component,这种全局可用。
二是局部注册方式
// 局部注册也可以这么做
var Parent = Vue.extend({
components: {
'my-component': {
template: '<div>A custom component!</div>'
}
}
})这种写法最简,很明显Parent扩展了Vue,拥有了组件my-component。此时的组件只有Parent能用,Vue不能用。
3.is属性
组件在使用的过程中也是有限制的。原因在于:
Vue 的模板是 DOM 模板,使用浏览器原生的解析器而不是自己实现一个。所以组件被替换后必须依照html的正常标准来,它必须是有效的 HTML 片段。一些 HTML 元素对什么元素可以放在它里面有限制。常见的限制:
a 不能包含其它的交互元素(如按钮,链接)
ul 和 ol 只能直接包含 li
select 只能包含 option 和 optgroup
table 只能直接包含 thead, tbody, tfoot, tr, caption, col, colgroup
tr 只能直接包含 th 和 td
以table为例
<table>
<my-component></my-component>
<my-component></my-component>
</table>
// 定义
var MyComponent = Vue.extend({
template: '<tr>A custom component!</tr>'
})这样的写法看似正常,因为<table><tr></tr></table>结构是正常的,但是实际上不能依赖自定义组件在浏览器验证之前的展开结果,所以这里不被认为是<tr>。为此,is属性便有作用了,将以上写法改写:
<table>
<tr is="my-component"></tr> //这里改成is属性
<tr is="my-component"></tr>
<tr is="my-component"></tr>
</table>
// 定义
var MyComponent = Vue.extend({
template: '<div>A custom component!</div>' //这里不能用tr
})修改后,相当于










