加入收藏 | 设为首页 | 会员中心 | 我要投稿 莆田站长网 (https://www.0594zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 搜索优化 > 正文

vue slot怎么在子组件中显示父组件传送

发布时间:2022-06-09 08:54:45 所属栏目:搜索优化 来源:互联网
导读:这次给大家带来vue slot怎么在子组件中显示父组件传递,vue slot在子组件中显示父组件传递的注意事项有哪些,下面就是实战案例,一起来看一下。 父组件使用没有指定slot属性,默认为default 在slot中可以使用默认值,如果父组件没有传递对应的slot,则会显
  这次给大家带来vue slot怎么在子组件中显示父组件传递,vue slot在子组件中显示父组件传递的注意事项有哪些,下面就是实战案例,一起来看一下。
 
  父组件使用没有指定slot属性,默认为default
 
  在slot中可以使用默认值,如果父组件没有传递对应的slot,则会显示默认值
 
  <!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <script src="vue.js" charset="utf-8"></script>
  </head>
  <body>
    <p id="app">
      <modal>
        <!-- 调用父组件的方法 -->
        <h1 @click='click'>aaa</h1></modal>
      <modal>
        <h2>bbb</h2></modal>
      <modal>
        <!-- 使用slot设置模板中的名字,会插入到指定的slot中 -->
        <p slot='title'>hello</p>
        <p slot='content'>
          world
        </p>
      </modal>
      <modal></modal>
    </p>
    <template id="modal">
      <!-- 使用slot在子组件中显示父组件传过来的模板 -->
        <p>
          modal
          <slot name='default'>如果没有会使用这个默认值</slot>
        <h1>
          title:
          <slot name='title'>
          </slot>
        </h1>
        content:
        <slot name='content'></slot>
        </p>
      </template>
    <script type="text/javascript">
      let modal = {
        template: '#modal'
      }
      new Vue({
        el: '#app',
        components: {
          // es 简写 ,只写一个的意思为
          // modal:modal
          modal
        },
        methods: {
          click() {
            console.log('aaa')
          }
        }
      })
    </script>
  </body>
  </html>
 
  推荐阅读:
 
  Vue.js的ul-li标签如何仿制select标签
 
  ajax动态赋值数据图
 
  以上就是vue slot怎么在子组件中显示父组件传递的详细内容。

(编辑:莆田站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读