将道具传递给Vue.js中的根实例

Xav*_*eña 1 javascript typescript vue.js

问题

我正在尝试将一个道具传递给我的根构造函数。我知道这propsData 是要走的路:

var appComponent = Vue.component('app', require('./components/app/app.vue.html'));
new Vue({
    el: '#app-root',
    router: new VueRouter({ mode: 'history', routes: routes }),
    propsData: { test: 'hi!' },
    components: { appComponent },
    render: h => h('app')
}); 
Run Code Online (Sandbox Code Playgroud)

这是AppComponent收到道具的:

export default class AppComponent extends Vue {

    @Prop()
    test: string;

    mounted() {
        console.log('test = ' + this.test);
        // result: test = undefined
        // expected: test = 'hi!'
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情

使它在开发中(而不是在生产中)工作的唯一方法是:

test: string;

beforeCreate() {
    this.test = this.$root.test;
}
Run Code Online (Sandbox Code Playgroud)

它可以在开发环境(Windows)上运行,但是在部署环境(linux)中,我收到此打字稿错误:

ERROR in [at-loader] ClientApp/components/app/app.ts:14:34
    TS2339: Property 'test' does not exist on type 'Vue'.
Run Code Online (Sandbox Code Playgroud)

这是另一篇关于此的文章。

Vam*_*hna 5

你可以通过props在data这第二个参数中的对象createElement别名h功能需要在render选项

render: h => h('app', { props: { test: 'hi!' }})
Run Code Online (Sandbox Code Playgroud)