• vue2入门--->非单文件组件(html直接使用组件)


    1.非单文件组件

    1.1 介绍

    1.使用步骤
    

    2. 几个注意点

    1.2 组件创建(定义)

    1.2.1 创建介绍

    1. 通过Vue.extend({})进行创建定义,可以省略为{}

    2.里面的内容几乎与vue示例里面的一样,正常写即可

    3.列子

    const student = Vue.extend({
       template:`
          

    学生姓名:{{studentName}}

    学生年龄:{{age}}

    `, data(){ return { studentName:'张三', age:18 } } })

    4.简写例子

     {
       template:`
          

    学生姓名:{{studentName}}

    学生年龄:{{age}}

    `, data(){ return { studentName:'张三', age:18 } } }

    5. export default

    暴露,导入的时候 from xx import xxx

    不暴露的话,无法导入

    1.2.2 代码示例

    1. //第一步:创建school组件
    2. const school = Vue.extend({
    3. template:`
    4. <div class="demo">
    5. <h2>学校名称:{{schoolName}}h2>
    6. <h2>学校地址:{{address}}h2>
    7. <button @click="showName">点我提示学校名button>
    8. div>
    9. `,
    10. // el:'#root', //组件定义时,一定不要写el配置项,因为最终所有的组件都要被一个vm管理,由vm决定服务于哪个容器。
    11. data(){
    12. return {
    13. schoolName:'尚硅谷',
    14. address:'北京昌平'
    15. }
    16. },
    17. methods: {
    18. showName(){
    19. alert(this.schoolName)
    20. }
    21. },
    22. })
    23. //第一步:创建student组件
    24. const student = Vue.extend({
    25. template:`
    26. <div>
    27. <h2>学生姓名:{{studentName}}h2>
    28. <h2>学生年龄:{{age}}h2>
    29. div>
    30. `,
    31. data(){
    32. return {
    33. studentName:'张三',
    34. age:18
    35. }
    36. }
    37. })
    38. //第一步:创建hello组件
    39. const hello = Vue.extend({
    40. template:`
    41. <div>
    42. <h2>你好啊!{{name}}h2>
    43. div>
    44. `,
    45. data(){
    46. return {
    47. name:'Tom'
    48. }
    49. }
    50. })

    1.3 注册

    1.3.1 介绍

    1.示例
    Vue.component('hello',hello)
    
    //创建vm
    new Vue({
       el:'#root',
       data:{
          msg:'你好啊!'
       },
       //第二步:注册组件(局部注册)
       components:{
          school,
          student
       }
    })

    2.组件的名称由components的key决定,如果key value一致,可以省略,写个key即可

    1.3.2 代码示例

    1. //第二步:全局注册组件
    2. Vue.component('hello',hello)
    3. //创建vm
    4. new Vue({
    5. el:'#root',
    6. data:{
    7. msg:'你好啊!'
    8. },
    9. //第二步:注册组件(局部注册)
    10. components:{
    11. school,
    12. student
    13. }
    14. })

    1.4 使用

    1. "root">
    2. <student>student>
  • 1.5. 完整实例

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>基本使用title>
    6. <script type="text/javascript" src="../js/vue.js">script>
    7. head>
    8. <body>
    9. <div id="root">
    10. <hello>hello>
    11. <hr>
    12. <h1>{{msg}}h1>
    13. <hr>
    14. <school>school>
    15. <hr>
    16. <student>student>
    17. div>
    18. <div id="root2">
    19. <hello>hello>
    20. div>
    21. body>
    22. <script type="text/javascript">
    23. Vue.config.productionTip = false
    24. //第一步:创建school组件
    25. const school = Vue.extend({
    26. template:`
    27. 学校名称:{{schoolName}}

    28. 学校地址:{{address}}

  • `,
  • // el:'#root', //组件定义时,一定不要写el配置项,因为最终所有的组件都要被一个vm管理,由vm决定服务于哪个容器。
  • data(){
  • return {
  • schoolName:'尚硅谷',
  • address:'北京昌平'
  • }
  • },
  • methods: {
  • showName(){
  • alert(this.schoolName)
  • }
  • },
  • })
  • //第一步:创建student组件
  • const student = Vue.extend({
  • template:`
  • 学生姓名:{{studentName}}

  • 学生年龄:{{age}}

  • `,
  • data(){
  • return {
  • studentName:'张三',
  • age:18
  • }
  • }
  • })
  • //第一步:创建hello组件
  • const hello = Vue.extend({
  • template:`
  • 你好啊!{{name}}

  • `,
  • data(){
  • return {
  • name:'Tom'
  • }
  • }
  • })
  • //第二步:全局注册组件
  • Vue.component('hello',hello)
  • //创建vm
  • new Vue({
  • el:'#root',
  • data:{
  • msg:'你好啊!'
  • },
  • //第二步:注册组件(局部注册)
  • components:{
  • school,
  • student
  • }
  • })
  • new Vue({
  • el:'#root2',
  • })
  • script>
  • html>
  • 2. 组件的嵌套

    2.1 代码示例

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>组件的嵌套title>
    6. <script type="text/javascript" src="../js/vue.js">script>
    7. head>
    8. <body>
    9. <div id="root">
    10. div>
    11. body>
    12. <script type="text/javascript">
    13. Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
    14. //定义student组件
    15. const student = Vue.extend({
    16. name:'student',
    17. template:`
    18. <div>
    19. <h2>学生姓名:{{name}}h2>
    20. <h2>学生年龄:{{age}}h2>
    21. div>
    22. `,
    23. data(){
    24. return {
    25. name:'尚硅谷',
    26. age:18
    27. }
    28. }
    29. })
    30. //定义school组件
    31. const school = Vue.extend({
    32. name:'school',
    33. template:`
    34. <div>
    35. <h2>学校名称:{{name}}h2>
    36. <h2>学校地址:{{address}}h2>
    37. <student>student>
    38. div>
    39. `,
    40. data(){
    41. return {
    42. name:'尚硅谷',
    43. address:'北京'
    44. }
    45. },
    46. //注册组件(局部)
    47. components:{
    48. student
    49. }
    50. })
    51. //定义hello组件
    52. const hello = Vue.extend({
    53. template:`<h1>{{msg}}h1>`,
    54. data(){
    55. return {
    56. msg:'欢迎来到尚硅谷学习!'
    57. }
    58. }
    59. })
    60. //定义app组件
    61. const app = Vue.extend({
    62. template:`
    63. <div>
    64. <hello>hello>
    65. <school>school>
    66. div>
    67. `,
    68. components:{
    69. school,
    70. hello
    71. }
    72. })
    73. //创建vm
    74. new Vue({
    75. template:'<app>app>',
    76. el:'#root',
    77. //注册组件(局部)
    78. components:{app}
    79. })
    80. script>
    81. html>

    3. 关于VueComponent

    3.1 介绍

    3.2 代码示例

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>VueComponenttitle>
    6. <script type="text/javascript" src="../js/vue.js">script>
    7. head>
    8. <body>
    9. <div id="root">
    10. <school>school>
    11. <hello>hello>
    12. div>
    13. body>
    14. <script type="text/javascript">
    15. Vue.config.productionTip = false
    16. //定义school组件
    17. const school = Vue.extend({
    18. name:'school',
    19. template:`
    20. 学校名称:{{name}}

    21. 学校地址:{{address}}

    22. `,
    23. data(){
    24. return {
    25. name:'尚硅谷',
    26. address:'北京'
    27. }
    28. },
    29. methods: {
    30. showName(){
    31. console.log('showName',this)
    32. }
    33. },
    34. })
    35. const test = Vue.extend({
    36. template:`atguigu`
    37. })
    38. //定义hello组件
    39. const hello = Vue.extend({
    40. template:`
    41. {{msg}}

    42. `,
    43. data(){
    44. return {
    45. msg:'你好啊!'
    46. }
    47. },
    48. components:{test}
    49. })
    50. // console.log('@',school)
    51. // console.log('#',hello)
    52. //创建vm
    53. const vm = new Vue({
    54. el:'#root',
    55. components:{school,hello}
    56. })
    57. script>
    58. html>

     

  • 相关阅读:
    Spring Boot 国际化踩坑指南
    linux锁与进程间通信
    共用一个一维空间的双向栈
    OpenHarmony 3.1 Beta版本关键特性解析——OpenHarmony图形框架
    Spring5 框架 ---- 事务操作
    Unity-2D像素晶格化消融
    MFC Windows 程序设计[136]之文件属性统计(附源码)
    Opus Security利用其AI驱动的多层次优先级引擎提升漏洞管理能力
    论文-分布式-分布式计算|容错-分布式控制下的自稳定系统
    金融机构数字化转型背景下,集中式与分布式存储选型之辨和未来之路
  • 原文地址:https://blog.csdn.net/qq_52385631/article/details/126681569