gpt4 book ai didi

javascript - 如何在 ui-router AngularJS 中使用 ui-sref

转载 作者:行者123 更新时间:2023-12-03 00:55:12 25 4
gpt4 key购买 nike

我有文件index.html:

<!DOCTYPE html>
<html>
<head>
<style>
.navbar { border-radius:0; }
</style>
<link rel="stylesheet" type="text/css" href="lib/css/bootstrap.min.css">
<script src="./node_modules/angular/angular.js"></script>
<script src="./node_modules/angular-ui-bootstrap/dist/ui-bootstrap-tpls.js"></script>
<script src="./node_modules/angular-ui-router/release/angular-ui-router.js"></script>
<script src="lib/js/app.js"></script>
</head>

<body ng-app="nav-module">
<a ui-view="home" ui-sref="home" ui-sref-active="active">home</a>
<a ui-view="about" ui-sref="about" ui-sref-active="active">about</a>
<ui-view>
</ui-view>

</body>
</html>

和文件app.js:

var app = angular.module("nav-module", ["ui.router",'ui.bootstrap']);
app.component('navComponent',{
templateUrl: './navbar.component.html'
});
app.config(['$stateProvider',
'$urlRouterProvider',function($stateProvider,$urlRouterProvider) {
$stateProvider
.state('/home',{
name: 'home page',
url:'/home',
template: '<h3>hello world!</h3>'
})
.state('/about',{
url:'/about',
component: 'navComponent'
});
$urlRouterProvider.otherwise('/home');
}]);

和nav-bar.component.html:

<h1>nav bar</h1>

但我无法单击标记来运行页面,并且当我更改 URL 中的/about 时,控制台显示错误:

Failed to load template: ./navbar.component.html

我不知道为什么。

最佳答案

我认为您缺少 templateurl 位置或文件名,因为您提到您的templateUrl:'./navbar.component.html' 但您的文件名是 nav-bar.component.html。

  1. 首先删除 templateUrl 并使用模板检查它:<h1>nav bar</h1>在组件中。
  2. 如果没问题,那么您的错误是因为缺少 templateUrl 引用或文件名。
  3. 然后使用 ../app/navtemplateurl.html 修复此问题,文件名为 navtemplateurl.html,位于应用程序文件夹内。

在模块中添加“ui.bootstrap.tpls”也可以解决此问题

更新:需要将 angularui 路由器引用从索引文件更改为 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.0-rc.1/angular-ui-router.js"></script>因为 v0.4.2 或更早版本不支持 ui-router 中的组件模板。您还需要更新此内容。

enter image description here

关于javascript - 如何在 ui-router AngularJS 中使用 ui-sref,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52867008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com