Angular UI-Router使用模板但不使用组件

时间:2016-10-17 15:11:29

标签: javascript angularjs angular-ui-router angular-components

我正在尝试在角度1项目上使用angular-ui-router。我遇到的问题是,当我指定模板时,我可以使用angular-ui-router工作,但在指定组件时则不行。

例如,这有效:

var groceryListRoutes = function($stateProvider) {
    var listOfGroceryLists = {
        name: 'listOfGroceryLists',
        url: '/lists',
        template: '<grocery-list-component></grocery-list-component>',
    };
    $stateProvider.state(listOfGroceryLists);
};

但是,当我尝试指定组件时,没有显示任何内容,并且控制台中没有给出错误:

var groceryListRoutes = function($stateProvider) {
    var listOfGroceryLists = {
        name: 'listOfGroceryLists',
        url: '/lists',
        component: 'groceryListComponent',
    };
    $stateProvider.state(listOfGroceryLists);
};

这是我的grocery-list.module.js,它注册了组件和路径:

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

import groceryListComponent from './grocery-list.component';
import groceryListAPIService from './grocery-list.service';
import groceryListRoutes from './grocery-list.routes';

import groceryListDetailComponent from './grocery-list-detail.component';

const GroceryListModule = angular.module('groceryList', [
    // Dependencies
    'ngResource',
    'ui.router',
])
    .config(($resourceProvider) => {
        $resourceProvider.defaults.stripTrailingSlashes = false;
    })
    .factory('groceryListAPIService', groceryListAPIService)
    .component('groceryListComponent', groceryListComponent)
    .component('groceryListDetailComponent', groceryListDetailComponent)
    .config(groceryListRoutes);

export default GroceryListModule;

我的grocery-list.component.js

import template from './grocery-list.template.html';

import groceryListController from './grocery-list.controller';

const groceryListComponent = {
    template,
    controller: groceryListController,
    controllerAs: 'groceryListCtrl',
}

export default groceryListComponent;

我的packages.json

{
  "name": "shopping-list",
  "version": "1.0.0",
  "description": "An app to keep track of your grocery shopping",
  "repository": "https://github.com/thomascothran/shopping_list.git",
  "scripts": {
    "start": "gulp",
    "test": "echo \"Error: no test specified\" && exit 1",
    "eslint": "eslint"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.17.0",
    "babel-eslint": "^7.0.0",
    "babel-loader": "^6.2.5",
    "babel-preset-es2015": "^6.16.0",
    "eslint": "^3.7.1",
    "eslint-config-airbnb": "^12.0.0",
    "eslint-plugin-import": "^1.16.0",
    "eslint-plugin-jsx-a11y": "^2.2.2",
    "eslint-plugin-react": "^6.3.0",
    "gulp": "^3.9.1",
    "raw-loader": "^0.5.1",
    "webpack": "^1.13.2",
    "webpack-stream": "^3.2.0"
  },
  "dependencies": {
    "angular": "^1.5.8",
    "angular-resource": "^1.5.8",
    "angular-ui-router": "^0.3.1",
    "js-cookie": "^2.1.3",
    "ramda": "^0.22.1"
  }
}

1 个答案:

答案 0 :(得分:1)

参考这个问题: Angular - UI.Router not loading component

根据你的package.json看起来你正在使用0.3.x,它不会起作用。升级到1.0.0,请尝试。

  

组件属性可以从ui-router@1.0.0获得(参见hereCHANGELOG.MD - 它是在1.0.0-aplpha中添加的)因此它不可用0.3。 1