首页 > 解决方案 > 无法使用 Vue-router 加载配置文件数据

问题描述

已解决: url: ('/api' +this.profilepath),

在我的项目中添加了 Vue-router,无法再加载配置文件数据。

---- 编辑---- this.$router.currentRoute.fullPath,不是给我完整路径。/api/ 不见了……


这是侧边栏组件:

 <li class="nav-item">
 <router-link :to="{ name: 'profile' , params:{id: this.profileId}}"
 class="nav-link"
 :key="$route.fullPath"

 >Profile</router-link>

我将配置文件 ID 传递给我的配置文件组件。

侧边栏组件通过 props 获取配置文件 ID:

 props: ['profileid'],

Profile组件正在生成axios“GET”路径:

      data() {
    return {
        status: 'Loading ...',
        profilepath: this.$router.currentRoute.fullPath,
        profile: {
        },
      }
      }, 

我在配置文件组件中“获取”配置文件数据的方法:

       methods: {

      loadProfile(){
        console.log(this.profilepath);
        this.status = 'Loading ....';
        axios({
         url: this.profilepath,
        method: 'get'
    })


          .then(response => {
          this.profile = response.data.profile;
          this.status = ('data received ....' );
          console.log('data received ....' +this.profile);
          this.status = 'Data loaded, succesfully!';
        })

这工作正常,现在只从我的 Laravel 后端接收一个空数组。我的请求 URL 正在传递配置文件 ID:http://192.168.10.101:8000/api/profile/1

问)在使用 Vue-router 链接时,我应该为 axios 添加一些额外的东西吗?

Error: in render: "TypeError: _vm.profile is undefined"

我从子组件 Userinfo 中找到了这个,它应该通过props: ['profile']

- 更新 - -

当我在方法内部进行故障排除时loadProfile()

console.log(this.profilepath);

它返回:/profile/1这是错误的!

问题是缺少'/api/',这就是为什么,我只收到一个空数组。

profilepath:this.$router.currentRoute.fullPath,不包括/api/。

标签: javascriptvue.jsaxiosvue-router

解决方案


推荐阅读