繁体   English   中英

MEAN角度ui路由器未加载状态

[英]MEAN angular ui-router doesn't load states

我正在使用meanstack建立网站。 但是,它无法加载我在app.js中定义的任何状态。 而且控制台中没有显示任何错误,并且每个js文件都已成功加载。 这是我的index.html:

<html lang="en">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <base href="/"></base>
    <title>E-study</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" type="text/css" href="angular-loading-bar/build/loading-bar.css">
    <link rel="stylesheet" type="text/css" href="bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="app.less">
    <!-- <link rel="stylesheet" type="text/css" href="app/_naut/less/styles.less"> -->
    </head>
    <body ng-app="E-study">
        <div id="main">
            <div id="topbar" class="topbar" style="width: auto;">
                <div class="fill">
                    <div class="container">
                        <h1 style="align: center;">E-study - a website for English self study</h1>
                    </div>
                </div>
            </div>
            <div data-ui-view data-autoscroll="false"  class="app-container ng-scope"></div>
       </div>
   </body>
</html>

app.js:

'use strict';

var Estudy = angular.module('E-study', ['ngCookies', 'ngResource', 'ngSanitize', 'ui.router', 'naut']);
Estudy.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', '$httpProvider', 'RouteProvider', function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider, Route){
    $stateProvider
    .state('login', {
        url: '/login',
        templateUrl: 'client/views/login.html',
        controller: 'loginCtrl'
    })
    .state('signup', {
        url: '/signup',
        templateUrl: 'client/views/signup.html',
        controller: 'signupCtrl'
    })
    .state('recover', {
        url: '/resetPwd',
        templateUrl: 'client/views/recover.html',
        controller: 'recoverCtrl'
    });
    $urlRouterProvider.otherwise('/login');
    $locationProvider.html5Mode(true);
    $httpProvider.interceptors.push('authInterceptor');
}])
.factory('authInterceptor', function ($rootScope, $q, $cookieStore, $location){
    return{
        request: function (config){
            config.headers = config.headers || {};
            if ($cookieStore.get('token')) {
                config.headers.Authorization = 'Bearer' + $cookieStore.get('token');
            };
            return config;
        },

        responseError: function (response){
            if (response.status === 401) {
                $location.path('/login');
                $cookieStore.remove('token');
                return $q.reject(response);
            } else {
                return $q.reject(response);
            };
        }
    };
})
.run(function($state){
     $state.go('login');  
 });
});

我在根目录中运行server.js,我项目的整个文件目录为: 文件目录

谢谢。

您需要在<html lang="en">标签中配置ng-app 并且您还需要调用(引用)要运行的index.html中的所有js文件。

如果您在3个js文件中注入了3个控制器。 您应该在index.js文件中引用该js文件。

您尚未将脚本包含在html ..那是导致错误的原因。

只需粘贴代码

<script src="/app.js"></script>

</head>标记之前,以便您的代码成为

<head>
<!---here leave the code same and just add this bottom script tag -->
<script src="/app.js"></script>
</head>

上面的代码仅在您的app.js位于/var/www/html目录中并且如果位于另一个目录中时才起作用,例如... /var/www/html/folder1/那么您必须使用。

<script src="/folder1/app.js"></script>

在您的index.html文件中添加此

<main ui-view></main>

我发现了问题,即我定义了重复路线

app.use('/', function(req, res, err){ res.sendFile('index.html', {root: path.join(__dirname, '../client')}) })

在routes.js文件中,并将其放在

app.route('/*').get(function (req, res){ res.sendFile('index.html', {root: path.join(__dirname, '../client')}); });

删除第一个代码后,它将正确加载状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM