作用:代码复用(代码中嵌入代码)

匿名插槽

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <!-- 3.使用子组件 -->
  12. <App></App>
  13. </div>
  14. <script src="./vue.js"></script>
  15. <script>
  16. Vue.component('MBtn',{
  17. template:`
  18. <button>
  19. <slot></slot>
  20. </button>
  21. `
  22. })
  23. const App = {
  24. data() {
  25. return {
  26. title: "老爹"
  27. }
  28. },
  29. template: `
  30. <div>
  31. <m-btn><a href="#">登录</a></m-btn>
  32. <m-btn>注册</m-btn>
  33. </div>
  34. `,
  35. }
  36. new Vue({
  37. el: '#app',
  38. data: {
  39. },
  40. components: {
  41. // 2.挂载子组件
  42. App
  43. }
  44. })
  45. </script>
  46. </body>
  47. </html>

具名插槽

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <!-- 3.使用子组件 -->
  12. <App></App>
  13. </div>
  14. <script src="./vue.js"></script>
  15. <script>
  16. // 只要匹配到slot标签的name值 template中的内容就会被插入到这个槽中
  17. Vue.component('MBtn', {
  18. template: `
  19. <button>
  20. <slot name='submit'></slot>
  21. <slot name='login'></slot>
  22. <slot name='register'></slot>
  23. </button>
  24. `
  25. })
  26. const App = {
  27. data() {
  28. return {
  29. title: "老爹"
  30. }
  31. },
  32. template: `
  33. <div>
  34. <m-btn>
  35. <template slot='submit'>
  36. 提交
  37. </template>
  38. </m-btn>
  39. <m-btn>
  40. <template slot='login'>
  41. <a href="#">登录</a>
  42. </template>
  43. </m-btn>
  44. <m-btn>
  45. <template slot='register'>
  46. 注册
  47. </template>
  48. </m-btn>
  49. </div>
  50. `,
  51. }
  52. new Vue({
  53. el: '#app',
  54. data: {
  55. },
  56. components: {
  57. // 2.挂载子组件
  58. App
  59. }
  60. })
  61. </script>
  62. </body>
  63. </html>

作用域插槽

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <!-- 3.使用子组件 -->
  12. <App></App>
  13. </div>
  14. <script src="./vue.js"></script>
  15. <script>
  16. // 已经开发了一个待办事项列表的组件,很多模块都在
  17. // A B
  18. // 1.之前数据格式和引用接口不变,正常显示
  19. // 2.新功能模块增加对勾
  20. const todoList = {
  21. data() {
  22. return {
  23. }
  24. },
  25. props: {
  26. todos: Array,
  27. defaultValue: []
  28. },
  29. template: `
  30. <ul>
  31. <li v-for='item in todos' :key='item.id'>
  32. <slot :itemValue = 'item'>
  33. </slot>
  34. {{item.title}}
  35. </li>
  36. </ul>
  37. `
  38. }
  39. const App = {
  40. data() {
  41. return {
  42. todoList: [{
  43. title: '大哥你好么',
  44. isComplate: true,
  45. id: 1
  46. },
  47. {
  48. title: '小弟我还行',
  49. isComplate: false,
  50. id: 2
  51. },
  52. {
  53. title: '你在干什么',
  54. isComplate: false,
  55. id: 3
  56. },
  57. {
  58. title: '抽烟喝酒烫头',
  59. isComplate: true,
  60. id: 4
  61. }
  62. ]
  63. }
  64. },
  65. components: {
  66. todoList
  67. },
  68. //v-slot='data'中的data表示插槽对象(封装了插槽属性和值)
  69. template: `
  70. <todoList :todos='todoList'>
  71. <template v-slot='data'>
  72. <input type="checkbox" v-model='data.itemValue.isComplate' />
  73. </template>
  74. </todoList>
  75. `,
  76. }
  77. new Vue({
  78. el: '#app',
  79. data: {
  80. },
  81. components: {
  82. App
  83. }
  84. })
  85. </script>
  86. </body>
  87. </html>