vue用computed获取姓和名

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="fun">
    <select name="" id="" v-model="full">
        <option>孙悟空</option>
        <option>猪八戒</option>
        <option>沙悟净</option>
    </select>
    <p>姓名:{{full}}</p>
    <p>姓:{{firstName}}</p>
    <p>名:{{lastName}}</p>
</div>
</body>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el: "#fun",
        data: {
            firstName: "孙",
            lastName: "悟空"
        },
        computed: {
            full: {
                get() {
                    return this.firstName + this.lastName;
                },
                set(val) {
                    let fullName = val;
                    this.firstName = fullName[0];   
                    this.lastName = fullName.slice(1,);
                }
            }
        }
    })
</script>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_43853424/article/details/88350979