Vue | 传参数

// 项目结构

├── index.html
├── main-view.js //父组件
├── desc-view.js //子组件
└── app.js

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>组件</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
</head>

<body>
    <div id="app">
        <main-view></main-view>
    </div>

    <script type="text/x-template" id="main-view">
        <div>
                <button @click="onChangeDescClick('Android')">Android</button>
                <button @click="onChangeDescClick('IOS')">IOS</button>
                <button @click="onChangeDescClick('Vue')">Vue</button>
                <desc-view  @showCityName="updateCity"  :pushSubDescObj='descObj'></desc-view>
        </div>
    </script>


    <script type="text/x-template" id="desc-view">
        <div>
                <h1>{{pushSubDescObj.title}}</h1>
                <p :style="{fontSize: pushSubDescObj.fontS + 'px'}">{{pushSubDescObj.desc}}</p>
                <button @click='select()'>放大字体</button>
        </div>
    </script>

    <script src="./app.js" type="module"></script>
    <script src="https://vuejs.org/js/vue.js"></script>
</body>

</html>

main-view.js

import {
    descView
} from './desc-view.js'
var mainView = {
    template: '#main-view',
    components: {
        descView
    },
    data: function () {
        return {
            descObj: {
                title: 'Android',
                desc: '这是Android的描述信息',
                fontS: 12
            }
        }
    },
    methods: {
        onChangeDescClick: function (type) {
            this.descObj.title = type;
            this.descObj.desc = '这是' + type + '的描述信息';
        },
        updateCity: function () {
            this.descObj.fontS++;
            console.log(this.descObj.fontS);
        }
    },
    create() {

    }
}

export {
    mainView
};

desc-view.js

var descView = {
    template: '#desc-view',
    props: {
    	//传多个参数
        pushSubDescObj: {
            type: Object,
            default: function () {
                return {
                    title: '这是一个默认的标题',
                    desc: '这是一个默认的内容',
                    fontS : 0
                }
            }
        }
    },
    methods: {
        select: function () {
            this.$emit('showCityName')
        }
    },
    create() {
        
    }
}

export {
    descView
};

app.js

//import {buttonSuccess} from './button-success.js';
import {
    mainView
} from './main-view.js';

var app = new Vue({
    el: '#app',
    components: {
        mainView
    }
})

坑!!!

emit()、props传多个参数

可能存在的报错

在这里插入图片描述

data里面没有return
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值