【问题标题】:Vue.js parent <-> child updating value, routing viewVue.js 父 <-> 子更新值,路由视图
【发布时间】:2019-04-06 10:25:56
【问题描述】:

我在子视图中更新父道具时遇到问题。在我的情况下,参赛者是父母,EditContestant 应该是孩子。 Contestant.vue 应该存储(是的,可能答案就在这里 - 存储在 Vuex 中)用户信息和 EditContestant 应该只呈现编辑视图,允许编辑用户信息操作并通过自己的方法或父方法保存它。我已经包含了路由,所以你应该明白我在说什么。

结构是:

Contestant.vue
|-EditContestant.Vue

参赛者.vue:

<script>
    export default {
        data() {
            return {
                contestant: {
                    id: this.$route.params.id,
                    gender: '',
                    firstname: '',
                    lastname: '',
                },
            }
        },
    methods: {
            fetchContestant(id) {
        //some api call here                            
              vm.updatePropContestant(res.data);
        // ...
            },
            updatePropContestant(newData) {
                if (!this.contestant || this.contestant.length === 0) {
                    this.contestant = {};
                }
                this.contestant.firstname = newData.firstname;
                this.contestant.lastname = newData.lastname;
                this.contestant.id = newData.id;
                this.contestant.gender = newData.gender;
                // this.$forceUpdate();
            },
        },
        }
</script>
<template>
    <div>
        <router-view :contestant="contestant"  @interface="contestant = $event"></router-view>
    </div>
</template>

EditContestant.vue

<script>
    export default {
        data() {
            console.log('data');
            return {
                editContestant: {
                    id: '',
                    'firstname': '',
                    'lastname': '',
                    'gender': 0
                },
                edit: false
            }
        },
        props: ['contestant'],
        created: function () {
// tried also to fetchContestant if parent beforeCreate method with same result, so tried moving it here :(
            this.$parent.fetchContestant(this.$route.params.id);
        }
    }
</script>

<template>
<div class="col">
<input id="firstname" v-model="this.$parent.contestant.firstname" type="text" class="form-control"                                placeholder="First name" 
<!-- this doesn't help either v-on:input="updateParentValue($event.target.value)" --> >
                </div>
</template>

app.js

require('./bootstrap');
window.Vue = require('vue');
import router from './router';
new Vue({
    el: "#app",
    router,
    // render: h => h(app)
});

路由器/index.js


import contestantComponent from '../components/Contestant/Contestant'
import contestantEditComponent from '../components/Contestant/EditContestant'
Vue.use(Router)
export default new Router({
    routes: [
        {
            path: '/contestant/:id',
            component: contestantComponent,
            children: [
                {

                    name: 'Contestant edit',
                    component: contestantEditComponent,
                    path: '/contestant/:id?/edit',
                },
                {
                    name: 'View contestant',
                    component: ViewContestantComponent,
                    path: '',
                }
            ],
    ]
})

当我在输入中添加一些东西时,我得到一个错误:


found in

---> <EditContestant> at resources/js/components/Contestant/EditContestant.vue
       <Contestant> at resources/js/components/Contestant/Contestant.vue
         <Root>
warn @ app.js:41366
logError @ app.js:42625
globalHandleError @ app.js:42620
handleError @ app.js:42580
invokeWithErrorHandling @ app.js:42603
invoker @ app.js:42916
original._wrapper @ app.js:48273

app.js:42629 TypeError: Cannot read property '$parent' of null
    at _c.on.input (app.js:37434)
    at invokeWithErrorHandling (app.js:42595)
    at HTMLInputElement.invoker (app.js:42916)
    at HTMLInputElement.original._wrapper (app.js:48273)

【问题讨论】:

  • 模板中不需要this.,只需使用$parent。除此之外,请始终尝试使用道具和事件,而不是直接通过this.$parent.function() 调用函数。你应该$emit('callFunc') 然后@callFunc="function"。可以尝试在 JsBin 中模拟吗?
  • @CloudSohJunFu 我会尝试把它放到网上。 this.$parent... 怎么会起作用?我的意思是它正在用实际的正确数据填充我的表单? :|编辑:哦男人/女人!我爱你!删除这个工作。感谢您的进一步提示。你会这么好心并解释一下在我的用例中在哪里使用 $emit 吗?有很多例子,但都是单文件模式。 Vue.js 允许的太多了。

标签: javascript vue.js routing parent-child


【解决方案1】:

模板中不需要this.,只需使用$parent。模板已经在 this. 范围内。

父子之间的通信应该使用Props down events up,而不是直接通过this.$parent.function()调用函数。你应该$emit('callFunc') 然后@callFunc="function"

【讨论】:

    【解决方案2】:

    从 EditContestatn.vue 文件中的 this.$parent... 中删除 this 有帮助。感谢您的帮助@CloudSohJunFu。如果您想回答我的问题,我很乐意接受您的回答并删除我自己的回答。

    【讨论】:

    • 我已经回答了这个问题,还有什么问题吗?
    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    相关资源
    最近更新 更多