【父子组件传值】/ 上一篇的代码补充

哎  我真是高看自己了 

copy来copy去写了一个css的布局  然后光说不练的看了几天视频 就以为自己什么都会了 然后放羊

其实呢-。- 万丈高楼平地起啊,根本自己写不下去,不练习自己也不care的话 真是等于0 了

比如父子组件传值 我1不会写 2 写过没存代码存了理论忘了 3 全网找不到验证过的 能用的代码 4  人很生气 进度也超慢

今天boss开会的时候说 “ 我在这给你们开个会 一万没了 你觉得呢”

“但是整个组方向错了 那没的是十万还要多”

 

enqwq   白看真的没用 对我

下面存几个链接 我也没实践 

这个组件写完之后 好好的整理一下(去博客园整理 csdn太水了点)  不能让博客没用了

https://blog.csdn.net/qq_40190624/article/details/90897949 父子传值

2 子向父传值  这个是可以用的 https://blog.csdn.net/qq_43642812/article/details/89492130

3 导航栏bug解决。没基础还是不要看好吧!https://blog.csdn.net/wang1006008051/article/details/78686451

https://juejin.im/post/5d705e166fb9a06ae372805e 看起来easy 不知道行不行

5 其他 https://www.zhihu.com/question/24696662

https://www.zhihu.com/question/30903863

这里补充一下代码吧

4-5 父子组件

<!DOCTYPE html>

<head>
  <meta charset="utf-8">
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
  <div id="root">
    <child content='Dell'></child>
    <child content='Lee'></child>
  </div>
</body>
<script>
  Vue.prototype.bus = new Vue()

  Vue.component('child', {
    data: function () {
      return {
        selfContent: this.content
      }
    },
    props: {
      content: String
    },
    template:
      '<div @click="handleClick">{{selfContent}}</div>',
    methods: {
      handleClick: function () {
        this.bus.$emit('change', this.selfContent)
      }
    },
    mounted: function () {
      var this_ = this;
      this.bus.$on('change', function (msg) {
        this_.selfContent = msg
      })
    },
  })

  var vm = new Vue({
    el: '#root',
  })
</script>

</html>

4-6 组件插槽

 

<!DOCTYPE html>

<head>
  <meta charset="utf-8">
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <title>组件插槽 qwq</title>
</head>

<body>
  <div id="root">
    <child>
            <div class='footer' slot="111" >我是foot 哦哦哦</div>
    </child>
  </div>
</body>
<script>
 
  Vue.component('child',{
    template:
    `<div>
      <slot name="222">
          <h1>default header</h1>
      </slot>
      <p>hello</p>
      <slot name="111">看我看我</slot>
    </div>`,
    props:['content'],
  })
  
  var vm = new Vue({
    el: '#root',
  })
</script>

</html>

4-7 作用域插槽

<!DOCTYPE html>

<head>
  <meta charset="utf-8">
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <title>作用域</title>
</head>

<body>
  <div id="root">
    <child>
      <template slot-scope='props'>
        <li>{{props.itemmmmm}}</li>
      </template>
    </child>
  </div>
</body>
<script>
 
  Vue.component('child',{
    data: function(){
      return {
        list: [1,2,3,4]
      }
    },
    template:
    `<div>child
      <ul>
        <slot v-for="item in list"
          :itemmmmm=item>
        </slot>
      </ul>
    </div>`,
  })
  
  var vm = new Vue({
    el: '#root',
  })
</script>

</html>

4-8 动态指令和v-once

<!DOCTYPE html>

<head>
  <meta charset="utf-8">
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <title>作用域</title>
</head>

<body>
  <div id="root">
    <component :is="typeeeee"></component>

    <child-one  v-if="typeeeee==='child-one'"></child-one>
    <child-two  v-if="typeeeee==='child-two'"></child-two>
    <button @click="handleBtnClick">change</button>
  </div>
</body>
<script>
 
  Vue.component('child-one',{
   template:'<div>child----one</div>'
  })

  Vue.component('child-two',{
   template:'<div>child----two</div>'
  })


  var vm = new Vue({
    el: '#root',
    data:{
      typeeeee:'child-one'
    },
    methods:{
      handleBtnClick: function(){
        this.typeeeee = (this.typeeeee==='child-one'? 'child-two':'child-one');
      },
    },

  })
</script>

</html>

 

 

1. 父组件向子组件传值,使用 props 属性 父组件: ``` <template> <div> <child-component :message="message"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { message: 'Hello, World!' } } } </script> ``` 子组件: ``` <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: ['message'] } </script> ``` 2. 子组件向父组件传值,使用 $emit 方法 父组件: ``` <template> <div> <child-component @send-message="handleMessage"></child-component> <p>{{ receivedMessage }}</p> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { receivedMessage: '' } }, methods: { handleMessage(message) { this.receivedMessage = message; } } } </script> ``` 子组件: ``` <template> <div> <button @click="sendMessage">Send Message</button> </div> </template> <script> export default { methods: { sendMessage() { this.$emit('send-message', 'Hello, World!'); } } } </script> ``` 3. 使用 provide/inject 传递数据 父组件: ``` <template> <div> <child-component></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, provide() { return { message: 'Hello, World!' } } } </script> ``` 子组件: ``` <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { inject: ['message'] } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值