【问题标题】:How to pass parameter to unnamed route: TypeError: Right-hand side of 'instanceof' is not callable如何将参数传递给未命名的路由:TypeError:'instanceof' 的右侧不可调用
【发布时间】:2020-10-31 21:11:57
【问题描述】:

我有一条路线:

router.js

       {
            path: "/books/new/:title",
            name: "new_book",
            component: () => 
import('./components/user/NewBookView.vue')
        }

我尝试通过路由器推送:

CreateBook.vue

this.$router.push({
    name: "new_book",
    params: {
         title: this.bookJson.data.title,
         book: this.bookJson.data
    }
});

但是我得到了一个

Error in nextTick: "TypeError: 
Right-hand side of 'instanceof' is not callable"

错误。

我需要将bookJson 作为参数传递给我的NewBookView.vue 组件:

NewBookView.vue

...
        data:  function()  {
            return {
                book: JSON
            }
        },
        created() {
            this.book = 
this.$route.params.book;
        }
...

我试过了

this.$router.push({
    path: "/books/new/" + 
this.booksJson.data.title,
    params: {
         book: this.bookJson.data
    }
});

book 参数似乎是undefined

所以似乎除非路由被命名,否则我无法传递参数,但如果它被命名,我也无法推送它。

编辑:我尝试将其更改为此,但仍然收到Error in nextTick: "TypeError: Right-hand side of 'instanceof' is not callable" 错误:

router.js

{
    path: "/books/new/:title",
    name: "new_book",
    component: () => import('./components/user/NewBookView.vue'),
    props: route => ({
        // set component property book to the route.params.book value
        book: route.params.book,
        ...route.params
    })
}

CreateBook.vue

this.$router.push({
    name: "new_book",
    params: {
         title: this.bookJson.data.title,
         book: this.bookJson.data
    }
});
})

NewBookView.vue

export default {
    props:  {
        book: JSON
    },
    created() {
        this.props.book = this.$route.params.book;
    }
}

【问题讨论】:

    标签: javascript vue.js vue-component vue-router


    【解决方案1】:

    使用props function mode

    这样设置你的路由器:

    {
        path: "/books/new/:title",
        name: "book",
        component: () => import("../pages/Book.vue"),
        props: route => ({
            // set component property book to the route.params.book value
            book: route.params.book,
            ...route.params
        })
    }
    

    不要使用data,而是在您的组件上使用props

    export default {
      name: "home",
      props: {
        title: String, 
        book: Object
      }
    };
    

    并像这样推送到您的路线:

    this.$router.push({
        name: "book",
        params: {
            title: "Foo Bar",
            book: {
                id: 1,
                title: "Foo Bar",
                author: "John Doe"
            }
        }
    });
    

    演示:https://codesandbox.io/s/vue-router-demo-3cfqs?file=/pages/Home.vue

    【讨论】:

    • 我尝试将我的代码更改为道具,但仍然得到相同的错误(见编辑)
    • JSON 不是有效的道具类型,请改用Object。检查可用类型here
    • 而且你不需要在 created 中设置 props 值。
    • 谢谢,成功了!你知道为什么会这样吗?为什么 Vue 决定不允许非命名路由有参数?为什么道具不能是JSON
    • 你仍然可以将参数传递给未命名的路由,但不能来自params,你需要像this.$router.push(`/books/new/${title}`)一样直接将路径连接为字符串。 Props 不能是 JSON,因为从技术上讲,javascript 中没有“JSON”类型,Object 尽可能接近 JSON。
    猜你喜欢
    • 2019-09-17
    • 2017-09-16
    • 2017-01-18
    • 2019-12-07
    • 1970-01-01
    • 2018-01-11
    • 2019-08-15
    • 2020-04-05
    • 1970-01-01
    相关资源
    最近更新 更多