Aurelia 路由:当我输入带有路由的 url 时无法加载页面 进一步阅读

问题描述

我是 Aurelia 框架的新手,正在使用他们网站上的联系人管理器教程。

当我从认 url 开始加载页面时:“http://localhost:9002”然后继续到“http://localhost:9002/contacts/2”它工作正常。但是,当我尝试仅使用 url "http://localhost:9002/contacts/2" 加载页面时,它无法加载,然后检查控制台 (F12) 我可以看到它无法加载多个 .css 和 .css 文件。 js 包。见下图。我能做什么?

enter image description here

解决方法

为什么会出现错误

发生此错误是因为您的浏览器会在“http://localhost:9002/contacts/”而不是“http://localhost:9002/”中搜索 .css 和 .js 文件,您的 SPA 在其中搜索这些文件来自。

使用 SPA 时,您应该通过在 s3Client .getObject(params,(err,data) => { if (err) console.log("There was an error finding object: ",err); }) .createReadStream() .on("error",(err) => { console.log("File Stream error: ",err); }) .on("data",(chunk) => { total += chunk.length; }) .on("end",() => { console.log("All done"); console.log("Total Streamed MB: ",total / 1000000); }) .pipe(response); 元素中添加 <base href="/"> 将页面的基本网址(通常)显式设置为 SPA 根,否则您的浏览器只会搜索与当前网址。

做什么

如果您在 Webpack 中使用最新的 <head>(本回答时为 v2.0.2),您的 aurelia-cli 文件应包含以下行;

index.ejs

从您的 <base href="<%- htmlWebpackPlugin.options.metadata.baseUrl %>"> 文件中的常量中提取其值。

webpack.config.js 中更新行

webpack.config.js

const baseUrl = '';

之后您的资源应该正确加载。

进一步阅读