js 实现vue—引入子组件props传参

参考:https://www.cnblogs.com/xiaohuochai/p/7388866.html

效果:


props传参.gif

html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <title>js实现vue—引入子组件props传参</title>
        <link rel="stylesheet" href="css/1.css">
        <script type="text/javascript" src="js/jquery.js"></script>
        <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
        <script src="https://cdn.staticfile.org/vue-router/2.7.0/vue-router.min.js"></script>
        <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
        <script src="js/1.js"></script>
    </head>
    <body>
        <div id="app">
            <keep-alive>
                <router-view class="child-view" v-if="$route.meta.keepAlive"></router-view>
            </keep-alive>

            <router-view class="child-view" v-if="!$route.meta.keepAlive"></router-view>
        </div>
        <script type="text/x-template" id="page1">
            <div>
                <TopNav :show-btn="ifShow"></TopNav>,
                <!-- 对于props声明的属性来说,在父级HTML模板中,属性名需要使用中划线写法 -->
                <p class="content">页面1</p>
                <router-link to="/page2" tag="span" class="btnRouter">页面2</router-link>
                <BlankPage id="BlankPage1"></BlankPage> 
                <!-- 多个组件引入同一组件改变显示/隐藏状态时,需绑定指定id,否则多个组件会混乱 -->         
            </div>
        </script>

        <script type="text/x-template" id="page2">
            <div>
                <TopNav :show-btn="ifShow"></TopNav>
                <p class="content">页面2</p>
                <BlankPage id="BlankPage2"></BlankPage>
            </div>
        </script>
    </body>
</html>

1.js:

$(document).ready(function() {
    Vue.prototype.$show = function(obj) { //全局函数1
        var o = $(obj);
        o.css('display', 'block');
    };
    Vue.prototype.$hide = function(obj) { //全局函数2
        var o = $(obj);
        o.css('display', 'none');
    };

    Vue.use(VueRouter);

    // 顶部组件  start
    var TopNav = Vue.extend({
        data() {
            return {
                showBtn: false
            }
        },
        props: ['showBtn'],
        watch: {
            showBtn: function(newVal, oldVal) {
                this.showBtn = newVal;
            }
        },
        template: "<p class='title'> " +
            "<span>顶部组件</span>" +
            "<span v-show='showBtn' class='btnBack' @click='$router.back(-1)'>返回</span>" +
            "</p>"
    })
    /* 子级props属性声明时,使用小驼峰或者中划线写法都可以;
       而子级模板使用从父级传来的变量时,需要使用对应的小驼峰写法
    */

    // 顶部组件  end

    // 正在加载组件  start
    var BlankPage = Vue.extend({
        template: "<div class='BlankPage'>" +
            "<div class='loadingDiv'>" +
            "<p class='loadingIcon'>" +
            "<img src='img/load.gif' alt=''>" +
            "</p>" +
            "<p class='loadingTxt'>正在加载...</p>" +
            "</div>" +
            "</div>"
    })
    // 正在加载组件  end

    // 页面1  start
    var Page1 = Vue.extend({
        data() {
            return {
                ifShow: false
            }
        },
        template: "#page1",
        // 局部注册子组件
        components: {
            TopNav,
            BlankPage
        }
    })
    //页面1  end

    //页面2  start
    var Page2 = Vue.extend({
        data() {
            return {
                ifShow: true
            }
        },
        template: "#page2",
        components: {
            TopNav,
            BlankPage
        }
    })
    //页面2  end

    var router = new VueRouter({
        routes: [{
                path: '/',
                name: 'Page1',
                meta: {
                    index: 0,
                    keepAlive: true,
                    title: '页面1'
                },
                component: Page1
            },
            {
                path: '/page2',
                name: 'Page2',
                meta: {
                    index: 1,
                    keepAlive: false,
                    title: '页面2'
                },
                component: Page2
            }
        ]
    })

    router.beforeEach((to, from, next) => {
        var toDepth = to.meta.index;
        var fromDepth = from.meta.index;

        if (to.meta.title) {
            document.title = to.meta.title;
        }

        if (toDepth == 'undefined' || toDepth == undefined) {
            if (true) {
                //这个可以关闭安卓系统的手机
                document.addEventListener('WeixinJSBridgeReady', function() {
                    WeixinJSBridge.call('closeWindow');
                }, false);
                //这个可以关闭ios系统的手机
                WeixinJSBridge.call('closeWindow');
                // wx.closeWindow();
            }
            return;
        } else if (toDepth < fromDepth) { //返回
            from.meta.keepAlive = false;
            to.meta.keepAlive = true;
        }
        next()
    })

    var app = new Vue({
        el: '#app',
        router
    }).$mount('#app')
})

1.css:

@CHARSET "UTF-8";

body {
    width: 100%;
    height: 100%;
}

body,
div,
p {
    margin: 0;
    padding: 0;
    text-align: center;
}

.content {
    margin-top: 200px;
}

.title {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 60px;
    padding-left: 50px;
    line-height: .60px;
    display: flex;
    align-items: center;
    color: #fff;
    background-color: lightseagreen;
    z-index: 1;
}

.btnBack {
    margin-left: 50%;
}

.btnRouter {
    width: 100px;
    height: 30px;
    line-height: 30px;
    margin-top: 20px;
    display: inline-block;
    background-color: lightseagreen;
    color: #fff;
    border-radius: 10px;
}

.NoMore {
    font-size: 14px;
    color: #888;
    margin-top: 30px;
    text-align: center
}

#NoMore1,
#NoMore2 {
    display: none;
}

.NoMoreTxt:before {
    content: "";
    width: 100px;
    height: 1px;
    display: inline-block;
    margin-bottom: 5px;
    margin-right: 1px;
    background-color: #dadada;
}

.NoMoreTxt:after {
    content: "";
    width: 100px;
    height: 1px;
    display: inline-block;
    background-color: #dadada;
    margin-bottom: 5px;
    margin-left: 10px;
}

#BlankPage1,
#BlankPage2 {
    display: none;
}

.BlankPage {
    width: 100%;
    height: 100%;
    font-size: 14px;
    color: #fff;
    background-color: #fff;
    -webkit-transition: all .2s ease-out;
    transition: all .5s ease-out;
    transition: all .5s ease-out;
    transition: all .5s ease-out;
    position: fixed;
    top: 0;
    z-index: 12;
}

.loadingDiv {
    position: fixed;
    top: 45%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 120px;
    height: 50px;
}

.loadingTxt {
    font-size: 14px;
    color: #666;
    text-align: center;
}

.loadingIcon {
    text-align: center;
}

.loadingIcon img {
    display: inline-block;
    width: 40%;
    height: 48px;
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,657评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,889评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,057评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,509评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,562评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,443评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,251评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,129评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,561评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,779评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,902评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,621评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,220评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,838评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,971评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,025评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,843评论 2 354

推荐阅读更多精彩内容