Angular服务工作,Angular构建不更新文件

时间:2018-05-24 14:26:25

标签: python django angular ng-build

我正在运行一个带有Angular前端和Django后端的项目。今天,当我更改了模板中的一些代码时,ng build没有更新。我发现它既不更新模板更改也不更新组件。但是,当我运行服务而是转到127.0.0.1:4200而不是Django端口8000时,将呈现新的更新版本。

我设置的方式是我有一个Django用TemplateViev指向的模板:

{% load static %}
{% csrf_token %}
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>AngularWebapp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
  <app-root></app-root>
{% block javascript %}
<script type="text/javascript" src="{% static 'runtime.js' %}"></script><script type="text/javascript" src="{% static 'polyfills.js' %}"></script><script type="text/javascript" src="{% static 'styles.js' %}"></script><script type="text/javascript" src="{% static 'vendor.js' %}"></script><script type="text/javascript" src="{% static 'main.js' %}"></script>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
{% endblock %}
</body>
</html>

静态目录布局在settings.py中是这样的:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'angular', 'static'),
    os.path.join(BASE_DIR, 'angular-webapp', 'dist', 'angular-webapp'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

和urls.py:

from django.contrib import admin
from django.urls import path, re_path, include
from django.views.generic import TemplateView
from rest_framework import routers
from authentication.views import AccountViewSet, LoginView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib.staticfiles import views

router=routers.SimpleRouter()
router.register('accounts', AccountViewSet)

class IndexView(TemplateView):
    template_name = 'index.html'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/', include(router.urls)),
    re_path(r'^api/v1/auth/login/$', LoginView.as_view(), name='login'),
    re_path(r'^.*/$', IndexView.as_view()),
    path('', IndexView.as_view()),
]

这是我唯一拥有静态文件的地方,也就是放置ng build的文件的地方,IE。静态加载从生成它时输出的文件夹中加载runtime.js等。

但是,从昨天开始,我在angular-webapp / src / app文件夹中对我的应用所做的更改在构建时没有更新。我已经尝试删除dist文件夹以创建一个新文件夹,但这并没有改变任何东西。当它回来并且我运行项目时它仍然以某种方式使用旧布局,而ng服务完美地工作。

关于ng build的工作原理我不知道是什么吗?

1 个答案:

答案 0 :(得分:2)

这可能是缓存破坏问题:您的文件仍然具有相同的名称,并且由于它们已被浏览器缓存,因此它不会重新加载它们。

考虑使用--prod标记进行构建,该标记包含其他几个标记,例如--aot,但要更正您的问题,请尝试使用--output-hashing=all进行构建。

直接来自ng build --help

--output-hashing=none|all|media|bundles 
  (String) Define the output filename cache-busting hashing mode.
  aliases: -oh <value>, --outputHashing <value>