【问题标题】:how to retrieve information in my edit page如何在我的编辑页面中检索信息
【发布时间】:2019-03-24 16:51:04
【问题描述】:

我想检索我的文章版本的信息,但遇到错误
我的控制器

   public function show($id){
   return new PostResource(post::where('id',$id)->findOrFail($id));
    }

myedit.vue

    export default {
        data() {
            return {
                form: new Form({
                    id: '',
                    title: '',
                })
            }
        },

        methods:{
            updateItem() {
                this.form.put('/api/orders/posts/' + this.form.id) 
            ....
        },
       created(){
axios.get(`/api/orders/posts/${id}`).then(({data}) => this.form.fill(data));
        }
    }

我有这个错误

[Vue warn]: Error in created hook: "ReferenceError: id is not defined"

found in

---> <UpdatePage> at resources/js/components/user/orders/post/Updatepage.vue
       <Root> app.js:112186:15
ReferenceError: "id is not defined"

【问题讨论】:

  • ${id} 来自哪里?是来自路由器params 还是其他?

标签: php laravel vuejs2


【解决方案1】:

试试这个

axios.get(`/api/orders/posts/${this.$router.params.id}`).then(({data}) => this.form.fill(data.data));

【讨论】:

    【解决方案2】:

    我认为您应该尝试替换该行

    axios.get(`/api/orders/posts/${id}`).then(({data}) => this.form.fill(data));
    

    这样

    axios.get(`/api/orders/posts/${this.$router.params.id}`).then(({data}) => this.form.fill(data));
    

    它将查找id 路由参数(如果您设置的名称不是id,则必须更改该参数的名称。我也会从mounted 而不是created 调用它(@987654327 @ 之前运行,将避免看到空表单)。

    此外,在您的控制器中,您可以编写更简单的代码形式:

    public function show($id){
       return new PostResource(post::findOrFail($id));
    }
    

    甚至

    public function show(post $post){
       return new PostResource($post);
    }
    

    如果设置正确。

    如果对你有帮助,请告诉我:)

    【讨论】:

    • 我试过了,我得到了这个错误:``` [Vue 警告]:挂载钩子中的错误:“ReferenceError:路由器未定义”```
    • 抱歉,这是您应该编写的更正代码行:axios.get(/api/orders/posts/${this.$router.params.id}).then(({data}) =&gt; this.form.fill(data));
    • 谢谢axios.get(/api/orders/posts/${this.$router.params.id}).then(({data}) =&gt; this.form.fill(data.data));
    • 我更新了我的答案,如果它解决了您的问题,您能否将该答案设置为有效?这将有助于其他有相同问题的开发人员确定正确的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2017-04-08
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多