javascript - Angular.js 应用程序:消除 #! 来自我的单个帖子的 URL,同时保持 slug
问题描述
我正在开发一个小型 AngularJS 博客应用程序(我使用的框架版本是 1.7.8)。
我已经设法从我自己制作的 API 中提取和显示帖子。
我在保留 slug 的同时#!
试图从我的单个帖子的 URL 中删除。
例子:
我有http://examplesite.eu/#!/europes-most-beautiful-castles
并且我试图得到http://examplesite.eu/europes-most-beautiful-castles
.
AngularJS 文档中的这个解决方案不起作用,因为它完全消除了蛞蝓,我想保留它,用于 SEO 目的:
appModule.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}]);
我的 app.js 文件:
angular.module('app', [
'ngRoute',
'app.controllers',
'ngSanitize'
]).config(['$routeProvider', function($routeProvider){
$routeProvider.when('/', {
templateUrl: 'templates/posts.html',
controller: 'PostsController'
}).when('/:slug', {
templateUrl: 'templates/singlepost.html',
controller: 'SinglePostController'
}).when('/page/:id', {
templateUrl: 'templates/page.html',
controller: 'PageController'
}).otherwise({
redirectTo: '/'
});
}]);
有什么替代方法可以将 slug 保留在帖子 URL 中?
解决方案
您需要使用$locationProvider.html5mode切换到历史 API,以便 Angular 可以更直接地操作 URL,而不是使用哈希。
推荐阅读
- java - Spring Boot 返回 404
- c++ - C++20 中的 constexpr std::vector 和 constexpr std::string
- python - 如何解决:IndexError:图像索引超出范围
- amazon-web-services - AWS S3 存储桶文件 - 将具有反斜杠的对象重命名为文件夹和文件
- java - Android 无法使用 Java 在套接字中发送两次 PrintWriter 消息。也可以发送 Python 代码而不是给我发送 Java 代码
- javascript - 使用 ES5 覆盖 toString - 奇怪的行为
- linux - 使用独立的 GCC 编译并作为 sysroot 主机
- pyomo - 警告:NEOS 暂时不可用。- 皮莫
- javascript - 任意循环内的 Webpack 动态导入
- c# - 从 C# 中的 api 访问 JSON 对象