2017-03-02 38 views
4

我有一个现有的django项目,我通过/ static/path提供静态文件。我正在将应用程序的一部分迁移到angular2,以便成为该网站的特定移动版本。使用angular-cli在Angular中向静态文件添加前缀路径

<base href="/">添加到index.html加载静态文件www.example.com/main.js在此获取404。 将<base href="/static/">添加到index.html可以正确加载静态文件,但是当应用程序启动时,它会将url更改为/ static /(我正在使用路由器),尽管我的url应该是/ order /。

是有使用基本href为/但添加前缀脚本和样式文件中使用的一种方式角CLI

使用的版本ng --version

@angular/cli: 1.0.0-rc.0 
node: 6.9.1 
os: win32 x64 
@angular/common: 2.4.9 
@angular/compiler: 2.4.9 
@angular/core: 2.4.9 
@angular/forms: 2.4.9 
@angular/http: 2.4.9 
@angular/platform-browser: 2.4.9 
@angular/platform-browser-dynamic: 2.4.9 
@angular/router: 3.4.9 
@angular/cli: 1.0.0-rc.0 
@angular/compiler-cli: 2.4.9 

回答

6

可以使用--deploy,网址前缀生成的静态文件的路径。 ng build -d /static/。它将生成的index.html前缀的js文件路径与/static/这样的:

<script type="text/javascript" src="/static/inline.bundle.js"></script> 

从帮助文档:

--deploy-url (String) URL where files will be deployed. 
    aliases: -d <value>, --deployUrl <value>