欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

uniapp 会将公共组件挂载到 App.vue 根目录,并对其进行渲染

最编程 2024-03-07 22:08:41
...

uniapp的根目录的template并没有暴露出来,所以无法通过在template中注册组件名的方式来注入组件,针对这种无tempalte的vue文件,可以使用new Vue() + render的方式挂载一个Vue组件,如下在App.vue中:
第一步,将mWaterText组件注册并挂载到#watermark中

// app.vue文件
import mWaterText from '@/components/m-water-text.vue'
import Vue from 'vue'

// 在onLaunch中


// 在app.vue挂载水印功能
new Vue({
    render: (createElement) => {
        return createElement(mWaterText, {
            props: {
                // 将变量name作为props传递给组件
                watermark: this.$store.state.vuex_runtimeData.name + '@' + this.$store.state.vuex_runtimeData.employeeNo
            }
        });
    }
}).$mount('#watermark');

第二部,去根目录下的template.h5.html的body标签中,注入一个div,加上id属性为watermark

// template.h5.html文件
<body>
    <div id="watermark"></div>