我的域:myname.com
我的 GitHub 存储库:myname
我的 GitHub 名称:myname
底层 GH 页面 URL: myname.github.io/myname
我的问题:我使用历史 Vue 路由器设置了以下页面:主页、联系人、项目。
每当我去时myname.com/contact
,它都会将我路由到 404。当我点击一个链接将我重定向到 时/contacts
,它会将它推送到地址栏,但如果您刷新,它会路由到 404。我已经阅读了诸如:将Vue 部署到 GitHub 页面。页面刷新时vue-router和Vue 路由器的错误得到 404,但问题仍然存在,并且自定义域增加了复杂性。我也读过这个:Vue Router 在重新访问 url 时返回 404但是,有一个 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: '/'
}
/src/router/index.js
,传入main.js
new Vue()
,并像这样导出:export default new VueRouter({
base: process.env.BASE_URL,
mode: 'history',
routes
})
App.vue
(入口组件)中,模板如下所示:<template>
<div id="app">
<nav-bar></nav-bar>
<router-view></router-view>
</div>
</template>
我无法通过 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>
在里面/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>
这对我有用,我现在只需输入myname.com/contact
. 如果您/public
使用相同的技巧将SPA 嵌套在里面,这也应该有效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句