簡體   English   中英

ui-router,無法導航到其他路由

[英]ui-router, can't navigate to another route

我在Mvc應用中遇到angularJs ui路由問題。 我已經看到有關此問題的幾個問題。 特別是這個問題似乎正在解決我的問題。 但是,我無法導航到其他路線。

在我的Views / Home / Index.cshtml中:

 @{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<base href="/">

<h1> Index Page</h1>

<a ui-sref="login">login</a>

<ui-view></ui-view>

在我的Angular / app.js中:

  var myApp = angular.module("myApp", ['ui.router']);

myApp.controller("LoginController", LoginController);


 myApp.config(function ($stateProvider, $urlRouterProvider, $locationProvider) {

     $stateProvider
         .state('login', {
             url: '/Login',
             templateUrl: '/home/Login',
             controller: LoginController,
             controllerAs: 'controller'
         })

            .state('Index', {
                url: '/',
                templateUrl: '/home/index'
            })
     // Handle request for non-existent route
     //$urlRouterProvider.otherwise('/home');

     $locationProvider.html5Mode({
         enabled: true,
         requireBase: false
     });


 });

在Login.cshtml中:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
} 
    <p> Login Page</p>  

<a ui-sref="Index">Index</a>

Route.config

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

在我的觀點/shared/_layout.cshtml中:

<body ng-app="myApp">

可能相關的問題:

$ locationProvider.html5Mode(true)和$ locationProvider.hashPrefix('!')。html5Mode(true)如何適合圖片?

您使用href導航到登錄狀態,這是錯誤的。 您應該改用ui-sref指令

<a ui-sref="login">login</a>

您還應該使用ui-view指令顯示您要定位的狀態。

官方文檔包含簡短的簡單教程 ,可幫助您入門。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM