简单理解其实组件就是制作自定义的标签,这些标签在HTML中是没有的。

组件注册的是一个标签,而指令注册的是已有标签里的一个属性。在实际开发中我们还是用组件比较多,指令用的比较少。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="../vue2.2.js"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
<!--<my-component></my-component>-->
<!--可重用性-->
<!--<my-component2></my-component2>-->
<!--此处不渲染-->
</div>
<!--<my-component></my-component>-->
<!--此处不渲染-->
<p>----------------分割线--------------------</p>
<div id="app2">
<my-component></my-component>
<my-component2></my-component2>
<my-component3></my-component3>
</div>
<my-component3></my-component3>
<script>
var myComponent = Vue.extend({
template: "<div>这是我的第一个component</div>"
}) //全局组件
Vue.component('my-component', myComponent) new Vue({
el: "#app"
})
var hello = {
template: "<div>这是我的第三个component</div>"
}
new Vue({
el: '#app2',
//局部组件
components: {
"my-component2": {
template: "<div>这是我的第二个component</div>"
},
"my-component3": hello
}
})
</script>
</body> </html>

全局注册:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="../vue2.2.js"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
</div>
<template id="myComponent">
<div>这是一个component
<p>123</p>
<a>456</a>
</div>
</template>
<script>
//全局注册
/*Vue.component("my-component",{
template:"#myComponent"
})*/
var vm = new Vue({
el: "#app",
components: {
"my-component": {
template: "#myComponent"
}
}
})
</script>
</body> </html>

局部注册:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="../vue2.2.js"></script>
</head>
<body>
<div id="app">
<parent-component></parent-component>
</div>
<script>
var Child = {
template:"<p>This is a child Component</p>"
}
//var Parent = Vue.extend()
Vue.component("parent-component",{
//局部注册child组件,child组件只能在parent组件内使用。
template:"<div><p>This is a parent Component</p><child-component></child-component></div>",
components:{
'child-component':Child
}
})
new Vue({
el:"#app"
})
</script>
</body>
</html>

最新文章

  1. 推荐12款实用的 JavaScript 书页翻转效果插件
  2. ios中的addChildViewController 和 android中的fragment
  3. Java Web的开始学习
  4. backbone杂记
  5. How I came to find Linux
  6. 小白日记28:kali渗透测试之Web渗透-扫描工具-Nikto
  7. Android开发之点击两次Back键退出App
  8. nyoj 915 +-字符串
  9. 成功启动了Apache却没有启动apache服务器
  10. Mysql安装时出现APPLY security settings错误
  11. Oracle 11g RAC 修改各类IP地址
  12. iOS开发之字数不一的多标签Demo
  13. MyEclipse打不开 报xxxxxx. log。
  14. vmware-tools安装——实用
  15. 20175311 2018-2019-2 《Java程序设计》第五周学习总结
  16. 使用vue之directive设计列表加载更多
  17. Linux——awk命令解析
  18. Java知多少(88)列表和组合框
  19. 初始JSP
  20. C++系统时间及字符串转换参考资料

热门文章

  1. C项目实践--家庭财务管理系统
  2. (22) java web的struts2框架的使用-struts配置文件
  3. RK3288-OTA编译失败解决办法【转】
  4. Get started with Sourcetree
  5. UWP开发入门系列笔记之(零):UWP的前世今生
  6. SPOJ:NT Games(欧拉函数)
  7. 【POJ 1947】 Rebuilding Roads
  8. 【HAOI 2008】 硬币购物
  9. 洛谷 P1311 选择客栈 —— 水题
  10. Velocity魔法堂系列一:入门示例(转)