我決定將現有的Angular 2應用程序集成到我的Django REST項目中。Django和Angular 2 templateUrl
創建Django應用程序與我的前端靜態文件夾:
urls.py:
from django.conf.urls import url from frontend import views from django.conf import settings from django.conf.urls.static import static urlpatterns = [ url(r'^$', views.index, name='index'), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
views.py:
from django.shortcuts import render def index(request): """ Renders the Angular2 SPA """ return render(request, template_name='index.html')
動我安固LAR 2應用前端的靜態文件夾
在我的設置中添加靜態設置:
STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'frontend', 'static'), ] STATIC_ROOT = os.path.join(BASE_DIR, 'static')
使index.html的變化:
<html> <head> <base href="/"> <title>PhotoHub</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> {% load staticfiles %} <link rel="stylesheet" href="{% static "node_modules/bootstrap/dist/css/bootstrap.min.css" %}"> <link href="{% static "bower_components/font-awesome/css/font-awesome.min.css" %}" rel="stylesheet" /> <link href="{% static "bower_components/alertify.js/themes/alertify.core.css" %}" rel="stylesheet" /> <link href="{% static "bower_components/alertify.js/themes/alertify.bootstrap.css" %}" rel="stylesheet" /> <link rel="stylesheet" href="{% static "styles.css" %}"> <script src="{% static "bower_components/jquery/dist/jquery.min.js" %}"></script> <script src="{% static "node_modules/bootstrap/dist/js/bootstrap.min.js" %}"></script> <script src="{% static "bower_components/alertify.js/lib/alertify.min.js" %}"></script> <!-- 1. Load libraries --> <!-- Polyfill(s) for older browsers --> <script src="{% static "node_modules/core-js/client/shim.min.js" %}"></script> <script src="{% static "node_modules/zone.js/dist/zone.js" %}"></script> <script src="{% static "node_modules/reflect-metadata/Reflect.js" %}"></script> <script src="{% static "node_modules/systemjs/dist/system.src.js" %}"></script> <!-- 2. Configure SystemJS --> <script src="{% static "systemjs.config.js" %}"></script> <script> System.import('app').catch(function(err){ console.error(err); }); </script> </head> <!-- 3. Display the application --> <body> <photohub></photohub> </body> </html>
做出改變
個systemjs.config.js
:(function (global) { System.config({ //static defaultJSExtensions: true, paths: { // paths serve as alias 'npm:': 'static/node_modules/' }, // map tells the System loader where to look for things map: { app: 'static/dist', // angular bundles '@angular/core': 'npm:@angular/core/bundles/core.umd.js', '@angular/common': 'npm:@angular/common/bundles/common.umd.js', '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', '@angular/http': 'npm:@angular/http/bundles/http.umd.js', '@angular/router': 'npm:@angular/router/bundles/router.umd.js', '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', // other libraries 'rxjs': 'npm:rxjs', 'angular-in-memory-web-api': 'npm:angular-in-memory-web-api', }, // packages tells the System loader how to load when no filename and/or no extension packages: { app: { main: './main.js', defaultExtension: 'js' }, rxjs: { defaultExtension: 'js' }, 'angular-in-memory-web-api': { main: './index.js', defaultExtension: 'js' } } }); })(this);
打字稿文件編譯在/dist
和模板留在/app
。當我用兩臺不同的服務器templateUrl
的路徑是這樣的:
templateUrl: './app/app.component.html'
現在我想聲明templateUrl
這種方式,但它不工作:
templateUrl: '{{ STATIC_URL }}' + '/app/app.component.html'
如何應對模板現在是網址嗎?
響應:
「GET /app/components/login/login.component.html HTTP/1.1」 404 2601
未找到:/app/app.component.html
JavaScript文件加載以及:
「GET /app/components/login/login.component.html HTTP/1.1」 404 2601
不缶nd:/app/components/register/register.component。HTML
這是一個很大的有用的信息,然後救助與*「不起作用」 *。 – jonrsharpe