vue2.0组件传值 bus

本文介绍了在Vue2.0中如何使用自定义事件总线(Bus)进行组件间的通信,详细阐述了Bus的创建和使用步骤,帮助开发者理解这一常见实践。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>bus</title>
    <style>
        .item {
            overflow: hidden;
            padding: 10px;
            width: 400px;
            border: 1px solid red;
        }

        .item img {
            width: 100px;
            float: left;

        }

        .film {
            border: 1px solid black;
            height: 1500px;
        }

        .filminfo {
            width: 300px;
            min-height: 200px;
            background: yellow;
            position: fixed;
            right: 50px;
            top: 100px;
            border: 1px solid blue;
        }
    </style>
    <script src="vue.js"></script>
</head>

<body>
    <div id="app">
        <button @click="getInfo">ajax</button>
        <movie v-for="item in movieList" :mymovie="item" :key="item.filmId"></movie>
        <moviedetail></moviedetail>    
    </div>
    <script>
        var bus = new Vue();
        Vue.component('movie', {
            template: `
            <div class="item">
                <img :src="mymovie.poster"/>
                <span>{
  {mymovie.name}}</span>
                <button @click="mvDetail()">详情</button>
            </div>`,
            props:["mymovie"],
            methods: {
                mvDetail () {
                    bus.$emit('Mdetail',this.mymovie.synopsis);
                }
            }
        })

        Vue.component("moviedetail",{
            data () {
                return {
                    Minfo: ''
                }
            },
            template: `
            <div class="filminfo">{
  {Minfo}}</div>`,
            mounted () {
                bus.$on("Mdetail",(data)=>{
                    console.log(data);
                    this.Minfo = data;
                })
            }
        })

        new Vue({
            el: '#app',
            data: {
                movieList: [],
            },
            methods: {
                getInfo () {
                    fetch("test.json")
                        .then(res => res.json())
                        .then(res => {
                            //console.log(res.data.films);
                            this.movieList = res.data.films
                        })
                },
                
                
            }
        })
    </script>
</body>

</html>

test.json

{
    "status": 0,
    "data": {
        "films": [{
            "filmId": 4645,
            "name": "尺八·一声一世",
            "poster": "https://pic.maizuo.com/usr/movie/c24fc5d02fa8df65456ee2a54d677e59.jpg",
            "actors": [{
                "name": "聿馨 ",
                "role": "导演",
                "avatarAddress": "https://pic.maizuo.com/usr/movie/c069afb64958d8eb1cbb57d822f5dbcb.jpg"
            }, {
                "name": "佐藤康夫",
                "role": " 自己",
                "avatarAddress": "https://pic.maizuo.com/usr/movie/f74cc4cacecf974cde84b51e86413471.jpg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 2.0 中父子组件之间的传值可以通过属性(prop)和事件(event)实现。 1. 属性(prop)传递值 父组件通过在子组件上定义prop属性,指定要传递的值类型和名字,然后将值传递给子组件的prop属性。子组件通过props接收到父组件传递的值,可以直接使用。 例如:父组件中定义一个名为message的prop属性,值为字符串类型,在模板中通过子组件标签向子组件传递值。 ``` // 父组件 <template> <div> <child-component :message="hello"></child-component> </div> </template> <script> import ChildComponent from './child-component.vue' export default { components: { ChildComponent }, data () { return { hello: 'hello, world' } } } </script> // 子组件 <template> <div>{{ message }}</div> </template> <script> export default { props: { message: { type: String, require: true } } } </script> ``` 2. 事件(event)传递值 父组件通过定义一个事件,在子组件中触发该事件并传递需要传递的值。子组件在父组件中使用子组件自定义的事件,并获取到传递的值。 例如:子组件定义一个自定义事件,触发时传递一个字符串类型的值。父组件中使用子组件,同时监听子组件的自定义事件,在事件回调函数中获取到子组件传递的值。 ``` // 子组件 <template> <button @click="changeMessage()">改变消息</button> </template> <script> export default { methods: { changeMessage () { this.$emit('change', '新消息') } } } </script> // 父组件 <template> <div> <child-component @change="handleChange"></child-component> <div>{{ message }}</div> </div> </template> <script> import ChildComponent from './child-component.vue' export default { components: { ChildComponent }, data () { return { message: '旧消息' } }, methods: { handleChange (newValue) { this.message = newValue } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值