Ale*_*ond 4 javascript github-pages vue.js vue-router
我的域名:myname.com
我的 GitHub 存储库:myname
我的 GitHub 名称:myname
底层 GH 页面 URL:myname.github.io/myname
我的问题:我使用 History Vue Router 设置了以下页面:主页、联系人、项目。
每当我访问 时myname.com/contact,它都会将我转至 404。当我点击一个链接将我重定向到 时/contacts,它会将其推送到地址栏,但如果刷新,它会路由到 404。我读过诸如以下的帖子:将 Vue 部署到 GitHub Pages。vue-router和Vue router 在页面刷新时出现 404错误,但问题仍然存在,并且自定义域增加了复杂性。我还读过这篇文章:Vue Router return 404 when revisit to the url但是,有一个 React Router 页面的示例,其中浏览器路由器在 GH-Pages 上工作: https: //milosrancic.github.io/reactjs-website/活动,回购: https: //github.com/milosrancic/reactjs-website。
这是我所做的:
package.json有以下行:"homepage": "https://myname.com/"vue.config.js正是这样:module.exports = {
publicPath: '/'
}
Run Code Online (Sandbox Code Playgroud)
/src/router/index.js,传递到main.js new Vue(),并像这样导出:export default new VueRouter({
base: process.env.BASE_URL,
mode: 'history',
routes
})
Run Code Online (Sandbox Code Playgroud)
App.vue(入口组件)内,模板如下所示:<template>
<div id="app">
<nav-bar></nav-bar>
<router-view></router-view>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
我无法通过 Vue 解决该问题,但正如tao提到的,这可能是 GitHub 及其处理 URL 的方式的问题。最简单的解决方法是使用哈希路由器,以便条目 URL 保持静态。然而,这会/#/在您的 URL 中引入可能令人厌恶的内容。
鉴于本机不支持包罗万象的路由,有人找到了解决方法: https: //github.com/rafgraph/spa-github-pages。注意:这可能不利于 SEO,因为预期的 URL 实际上并不存在。这是利用 404 重定向并在索引页面上处理它的技巧。这是一个投资组合网站,因此,我现在对此表示同意。如果我或其他人找到更好的解决方案,将会更新。
解决方法:
在里面/public添加一个名为的文件404.html并粘贴以下内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CHANGE THIS TO YOUR TITLE</title>
<script type="text/javascript">
// MIT License
// https://github.com/rafgraph/spa-github-pages
// This script takes the current url and converts the path and query
// string into just a query string, and then redirects the browser
// to the new url with only a query string and hash fragment,
// e.g. https://www.foo.tld/one/two?a=b&c=d#qwe, becomes
// https://www.foo.tld/?/one/two&a=b~and~c=d#qwe
// Note: this 404.html file must be at least 512 bytes for it to work
// with Internet Explorer (it is currently > 512 bytes)
// If you're creating a Project Pages site and NOT using a custom domain,
// then set pathSegmentsToKeep to 1 (enterprise users may need to set it to > 1).
// This way the code will only replace the route part of the path, and not
// the real directory in which the app resides, for example:
// https://username.github.io/repo-name/one/two?a=b&c=d#qwe becomes
// https://username.github.io/repo-name/?/one/two&a=b~and~c=d#qwe
// Otherwise, leave pathSegmentsToKeep as 0.
var pathSegmentsToKeep = 0;
var l = window.location;
l.replace(
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
l.hash
);
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在里面,在后面/public/index.html添加以下内容:<head><title>
<script type="text/javascript">
// MIT License
// https://github.com/rafgraph/spa-github-pages
// This script checks to see if a redirect is present in the query string,
// converts it back into the correct url and adds it to the
// browser's history using window.history.replaceState(...),
// which won't cause the browser to attempt to load the new url.
// When the single page app is loaded further down in this file,
// the correct url will be waiting in the browser's history for
// the single page app to route accordingly.
(function(l) {
if (l.search[1] === '/' ) {
var decoded = l.search.slice(1).split('&').map(function(s) {
return s.replace(/~and~/g, '&')
}).join('?');
window.history.replaceState(null, null,
l.pathname.slice(0, -1) + decoded + l.hash
);
}
}(window.location))
</script>
Run Code Online (Sandbox Code Playgroud)
这对我很有用,我现在只需输入即可访问联系人页面myname.com/contact。/public如果您使用相同的技巧在内部嵌套了 SPA,这也应该有效。
| 归档时间: |
|
| 查看次数: |
1738 次 |
| 最近记录: |