【问题标题】:vue.js this.$router undefinedvue.js this.$router 未定义
【发布时间】:2018-04-09 13:44:52
【问题描述】:

我越来越不确定

this.$router

在 App.vue 和其他组件中未定义。我找不到解决方案。

这里是我的 main.js 的一部分

Vue.use(VueRouter);
Vue.use(VueResource);

const router = new VueRouter({
    routes: Routes,
    mode: 'history'
});

new Vue({
    el: '#app',
    render: h => h(App),
    router
});

这里是我在 main.js 中导入的 router.js。

export default [
    {path: '/', component: Home, name: 'home'},
    {path: '/home', component: Home, name: 'home2'},
    {path: '/user/login', component: Login, name: 'userLogin'}
];

谢谢你:)

编辑

我在这样的脚本标签中使用它,

<script>
    import HeadNavBar from './components/HeadNavBar.vue';
    import SideBarNav from './components/SideBarNav.vue';
    import Home from './components/Home.vue';


    console.log(this.$router,pus); //the undefined type

    export default {
        name: 'app',
        data() {
            return {
                isLogin: true
            }
        },
        components: {
            'app-headnav': HeadNavBar,
            'app-sidebarnav': SideBarNav,
            'app-home': Home
        }

    }
</script>

但是当我将它移到方法部分时,我得到了我想要的响应。

export default {
    name: 'app',
    data() {
        return {
            isLogin: true
        }
    },
    components: {
        'app-headnav': HeadNavBar,
        'app-sidebarnav': SideBarNav,
        'app-home': Home
    },methods: {
        myFunc: function() {
            console.log(this.$router,pus); // this gave result
        }
    }
}

【问题讨论】:

  • 问题可能在于您对this 的使用(也许您正在使用箭头函数)。请更新问题以显示更多上下文以帮助解决问题。
  • 谢谢 :) 我在组件的脚本标签中使用它,比如编码纯 js。但是当我使用该方法时,我得到了我应该有的反应。

标签: javascript vue.js vuejs2 vue-router


【解决方案1】:

我只能在使用arrow function 时为this.$router 重现undefined 错误(也许这就是您正在做的事情),Vue documentation 建议不要这样做:

不要在选项属性或回调上使用箭头函数,例如 created: () =&gt; console.log(this.a)vm.$watch('a', newValue =&gt; this.myMethod())。由于箭头函数绑定到父上下文,this 不会像您期望的那样成为 Vue 实例,通常会导致诸如 Uncaught TypeError: Cannot read property of undefinedUncaught TypeError: this.myMethod is not a function 之类的错误。

这是一个mounted 回调成功记录this.$router 的示例:

<script>
  export default {
    name: 'app',

    // DON'T USE ARROW FUNCTIONS HERE
    // mounted: () => {
    //   console.log({router: this.$router});
    // },

    mounted() {
      console.log({router: this.$router});
    }
  }
</script>

【讨论】:

    【解决方案2】:

    我们有同样的错误,在这种情况下我已经修复了,我只是像这样在我的组件中导入路由器文件并且它正在工作:

    在我的组件中导入路由器文件/配置:

    import router from '../router'
    

    我可以这样使用:

    router.replace('home')
    

    这应该可以工作

    我的完整代码:

    <script>
    import router from '../router'
    export default {
      methods: {
      signIn: function () {
        firebase
        .auth().signInWithEmailAndPassword(this.email, this.password).then(
          function (user) {
            router.replace('home')
          },
          function (err) {
            alert('Maaf, ' + err.message)
          }
        )
      }
     }
    }
    </script>
    

    【讨论】:

    【解决方案3】:

    我知道这已经过时了,但我会分享我遇到的一些事情以及我是如何解决它的,以防有人在那里挣扎。

    我的场景是一个 vue/meteor 项目。

    在函数/处理程序中,“this”指的是函数的目标,因此 this.$router 是在函数或目标本身中寻找 $router。

        ///////DOESN'T WORK ///////////
      mounted: function () {
        $(document).on('click', '.element', function(){
                 //this is recognized as 
                 this.$router.push(PATH)
            });
      },
    
    
        ////////THIS WORKS//////////
      mounted: function () { 
        //make router defined outside of function.
        var navigate = this.$router;
    
         $(document).on('click', '.element', function(){
            navigate.push(PATH)
         });
      }
    

    【讨论】:

      【解决方案4】:

      export上方的console.log(this.$router,pus)在导入组件时执行。它发生在组件创建之前。所以this 只是一个空对象{}

      您应该在导出对象的方法中获取this.$router

      【讨论】:

        猜你喜欢
        • 2020-02-15
        • 2018-06-03
        • 2019-03-18
        • 2019-08-11
        • 2018-11-23
        • 2020-08-07
        • 2016-08-21
        • 2018-10-06
        • 2018-09-19
        相关资源
        最近更新 更多