Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 我有一个路由组件,为什么当我在路由定义中设置道具时,道具不会自动更新?_Vue.js_Vue Router_Vue Props - Fatal编程技术网

Vue.js 我有一个路由组件,为什么当我在路由定义中设置道具时,道具不会自动更新?

Vue.js 我有一个路由组件,为什么当我在路由定义中设置道具时,道具不会自动更新?,vue.js,vue-router,vue-props,Vue.js,Vue Router,Vue Props,我有一本列出所有注册图书的索引书,当我点击编辑一本指定图书时,它会在路径中获取指定图书的id,但我的props元素没有获取它,这会导致错误! 我怎样才能修好它 下面是我的索引页上的代码: 在我的更新页面中,我有: <script> props: ['id'], data() { return { books: [], }; }, methods: { getBook(id) { axios.get(`http://localhost:3000/ap

我有一本列出所有注册图书的索引书,当我点击编辑一本指定图书时,它会在路径中获取指定图书的id,但我的props元素没有获取它,这会导致错误! 我怎样才能修好它

下面是我的索引页上的代码:


在我的更新页面中,我有:

<script>
 props: ['id'],
 data() {
  return {
   books: [],
  };
 },
 methods: {
  getBook(id) {
   axios.get(`http://localhost:3000/api/v1/books/${id}`) // Gets an specif book
    .then((response) => { this.books = response.data; });
 },
 created() {
  this.getBook(this.id);
 },
 watch: {
  $route() {
   this.getBook(this.id);
 },

道具:['id'],
数据(){
返回{
书籍:[],
};
},
方法:{
getBook(id){
axios.get(`http://localhost:3000/api/v1/books/${id}`//获取一个指定的书籍
.then((response)=>{this.books=response.data;});
},
创建(){
this.getBook(this.id);
},
观察:{
$route(){
this.getBook(this.id);
},

路由器不会将
id
传递给道具。在您的情况下,您可以在
中找到
id
。$route.params

即使您在路由器本身中使用类似于
/book/:id
的东西,该
id
param也将存在于
this.$route.params.id

所以你需要一些类似的东西:

 created() {
  this.getBook(this.$router.params.id);
 },
顺便说一句,在《观察者》中也是如此


您能显示定义路由的代码吗?我的理解是,只有当它是“路由组件”时,参数才会自动设置。请参见示例。{path:'/update\u book/:id',name:'update\u book',component:UpdateBook,props:true,},为什么id应该在那里?params不向props传递任何东西你是什么意思?我得到这个错误“TypeError:this.$router.params未定义”我的错。
这。$route
不是routeRno问题兄弟,mb最好将问题改为“如何在vue路由器中从route获取参数”?是的,我认为这是可行的。我认为原始问题可以更好地表述为:“我有一个,为什么我的道具在路由定义中设置时不会自动更新?”答案还有待观察。同意来自城市的先生的意见。我可以编辑它以获得一些廉价的业力吗?)