vue常用创建组件几种方式总结

最近一周需要使用vue开发一个谷歌扩展插件,但是又不能在vue-cli脚手架中开发,所以只能单独引入vue.js整个包进行脚本植入开发。引入vue.js就代表着不能用import、require之类的引入单文件组件文件,只能在文件中开发,或者多个js文件分先后顺序植入开发,然后就出现了一个尴尬的问题就是,忘记了最原本的其他组件创建的方式,所以记录回顾下:

1. 全局注册组件:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=“UTF-8”>
  5. <title> </title>
  6. </head>
  7. <body>
  8. <div id=“app”>
  9. <my-component> </my-component>
  10. </div>
  11. <template id=“myComponent”>
  12. <div>This is a component! </div>
  13. </template>
  14. </body>
  15. <script src=“js/vue.js”> </script>
  16. <script>
  17. Vue.component( ‘my-component’,{
  18. template: ‘#myComponent’,
  19. data(){
  20. return {}
  21. },
  22. methods: {
  23. }
  24. })
  25. new Vue({
  26. el: ‘#app’
  27. })
  28. </script>
  29. </html>

2. 局部注册组件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=“utf-8”>
  5. <title>局部注册组件 </title>
  6. </head>
  7. <body>
  8. <section id=“app”>
  9. <my-component-a> </my-component-a>
  10. <my-component-b> </my-component-b>
  11. </section>
  12. <template id=“a”>
  13. <h1>这是一个A组件 </h1>
  14. </template>
  15. <template id=“b”>
  16. <h2>这是一个B组件 </h2>
  17. </template>
  18. <script type=“text/javascript” src=“js/vue.js”> </script>
  19. <script type=“text/javascript”>
  20. const componentA = {
  21. template: ‘#a’,
  22. data(){
  23. return { }
  24. }
  25. //…
  26. }
  27. const componentB = {
  28. template: ‘#b’,
  29. data(){
  30. return { }
  31. }
  32. //…
  33. }
  34. var vm = new Vue({
  35. el: ‘#app’,
  36. components: {
  37. // 局部注册,my-component-a是标签名称
  38. ‘my-component-a’: componentA,
  39. // 局部注册,my-component-b是标签名称
  40. ‘my-component-b’: componentB
  41. }
  42. })
  43. </script>
  44. </body>
  45. </html>

3. 使用x-template引入模板

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=“utf-8”>
  5. <title>x-template注册组件 </title>
  6. </head>
  7. <body>
  8. <script type=“text/javascript” src=“js/vue.js”> </script>
  9. <script type=“text/x-template” id=“checkbox-template”>
  10. <div class=“checkbox-wrapper” @click=“check”> <div :class="{ checkbox: true, checked: checked }"> </div> <div class=“title”> </div> </div>
  11. </script>
  12. <script type=“text/javascript”>
  13. Vue.component( ‘my-checkbox’, {
  14. template: ‘#checkbox-template’,
  15. data() {
  16. return { checked: false, title: ‘Check me’ } },
  17. methods: {
  18. check() {
  19. this.checked = ! this.checked;
  20. }
  21. }
  22. });
  23. </script>
  24. </body>
  25. </html>

4. render函数注册组件( 类似于虚拟DOM的实现 )

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=“utf-8”>
  5. <title>render注册组件 </title>
  6. </head>
  7. <body>
  8. <my-checkbox> </my-checkbox>
  9. <script type=“text/javascript” src=“js/vue.js”> </script>
  10. <script type=“text/javascript”>
  11. Vue.component( ‘my-checkbox’, {
  12. data() {
  13. return {
  14. checked: false,
  15. title: ‘Check me’
  16. }
  17. },
  18. methods: {
  19. check() {
  20. this.checked = ! this.checked;
  21. }
  22. },
  23. render(createElement) {
  24. return createElement( ‘div’, {
  25. attrs: {
  26. ‘class’: ‘checkbox-wrapper’
  27. },
  28. on: {
  29. click: this.check
  30. }
  31. },
  32. [
  33. createElement( ‘div’, {
  34. ‘class’: {
  35. checkbox: true,
  36. checked: this.checked
  37. }
  38. }),
  39. createElement( ‘div’, {
  40. attrs: {
  41. ‘class’: ‘title’
  42. }
  43. }, [ this.title] )
  44. ]);
  45. }
  46. });
  47. </script>
  48. </body>
  49. </html>

5. jsx注册组件

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=“utf-8”>
  5. <title>jsx注册组件</title>
  6. </head>
  7. <body>
  8. <my-checkbox></my-checkbox>
  9. <script type=“text/javascript” src=“js/vue.js”></script>
  10. <script type=“text/javascript”>
  11. Vue.component(‘my-checkbox’, {
  12. data() {
  13. return {
  14. checked: false,
  15. title: ‘Check me’
  16. }
  17. },
  18. methods: {
  19. check() {
  20. this.checked = !this.checked;
  21. }
  22. },
  23. render() {
  24. return <div class = "checkbox-wrapper"onClick = { this.check}>
  25. <div class = {
  26. {
  27. checkbox: true,
  28. checked: this.checked
  29. }
  30. }></div>
  31. <div class=“title”>{ this.title }</div>
  32. </div> } });
  33. </script>
  34. </body>
  35. </html>

其他组件相关知识点总结:

1. Vue.extend

概述:Vue.extend返回的是一个“扩展实例构造器”,也就是预设了部分选项的Vue的实例构造器,它常常服务于Vue.component用来生成组件,可以简单理解为当在模板中遇到该组件作为标签的自定义元素时,会自动调用“扩展实例构造器”来生产组件实例,并挂在到自定义元素上

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=“utf-8”>
  5. <title>Vue.extend使用 </title>
  6. </head>
  7. <body>
  8. <author> </author>
  9. <div id=“author”> </div>
  10. <script type=“text/javascript” src=“js/vue.js”> </script>
  11. <script type=“text/javascript”>
  12. //挂载到标签为author自定义标签上
  13. var author1 = Vue.extend({
  14. template: “<p><a :href=‘url’>{{author}}</a></p>”,
  15. data : function() {
  16. return {
  17. author : ‘vamous’,
  18. url : ‘http://blog.csdn.net/Dear_Mr/article/details/72614370’
  19. }
  20. }
  21. });
  22. new author1(). KaTeX parse error: Expected '}', got '&' at position 956: …"hljs-string">"&̲lt;p&gt;&lt;a :…mount( ’#author’);
  23. </script>
  24. </body>
  25. </html>

2. Vue.extend 和 Vue.component的区别和联系

extend 是构造一个组件的语法器. 
你给它参数 他给你一个组件 然后这个组件

你可以作用到Vue.component 这个全局注册方法里, 也可以在任意vue模板里使用car组件

  1. var car = Vue.extend({ 
  2.     //…. 
  3.  }) 
  4.  Vue.component( ‘car’,car)

你可以作用到vue实例或者某个组件中的components属性中并在内部使用car组件

  1. new Vue({ 
  2.       components:{ 
  3.         car:car 
  4.       } 
  5.    })

Vue.component 你可以创建 ,也可以取组件 例如下

var car = Vue.component(‘car’)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值