SPA应用一般有两种:html5 history模式( http://localhost:4200/user
),看起来更加美观,然而如没有后台支持,刷新页面就会报:Cannot GET /pagurl之类的错,一种是Hash模式( http://localhost:4200/#/user
)。
权衡之后,决定将模式改为Hash模式:
- 引入所需的包:
-
import {HashLocationStrategy, LocationStrategy} from '@angular/common';
- 放到App-module里的providers里
-
providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],
- npm start运行即可