$ state.go不使用angularjs 1.5

时间:2016-08-25 14:31:32

标签: javascript angularjs

我想在我的应用程序angularjs配置路由这是我的app.js

在我的app.js中我有两个chlidren登录和home bu默认我有loginComponent 然后当我点击按钮登录时,我将主视图替换登录视图

我有一个错误$ state未定义

   import AppComponent from './app.component';
    import LoginComponent from './login/login.component';
    import HomeComponent from './home/home.component';

    import angular from 'angular';
import uiRouter from 'angular-ui-router';

angular.module('app', [
  uiRouter
])
  .config(($stateProvider, $urlRouterProvider)=> {
    "ngInject";
    $urlRouterProvider.otherwise('/');
    $stateProvider
      .state('login', {
        url: '/',
        component: 'login'
      });
    $stateProvider
      .state('home', {
        url: '/home',
        component: 'home'
      });
  })
  .component('app', AppComponent)
  .component('home', HomeComponent)
  .component('login', LoginComponent);

这是我的login.controller

class LoginController {
  constructor() {
    this.name = 'login';
    console.log('login controller');
  }
  openHome(){
    console.log("uo");
    $state.go('home');
  }
}

export default LoginController;

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:3)

正如评论中所述,您需要将$state注入LoginController

class LoginController {
  constructor($state) {
    this.name = 'login';
    this.$state = $state;
    console.log('login controller');
  }
  openHome(){
    console.log("uo");
    this.$state.go('home');
  }
}

LoginController.$inject = ['$state'];

export default LoginController;

或者您可以使用ng-annotate

之类的内容