Angular 2-在生产模式下刷新页面404,未配置useHash

时间:2019-03-08 10:02:13

标签: angular typescript

我正在使用Angular 2,但没有使用useHash

我发现无法直接在浏览器中访问该URL,这将返回404 not found。

我在Google上搜索了很多,并尝试了以下方法:

  1. LocationStrategy添加PathLocationStrategy
  2. loadChildren更改为相对路径。

但是没有一个。

有人可以帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:0)

如果我对它的理解不好。您需要在import sys import re import os from collections import defaultdict import nbformat from nbconvert import LatexExporter, exporters OUTPUT_FILES_DIR = './images' CUSTOM_TEMPLATE = 'style_ipython_custom.tplx' MAIN_TEX = 'main.tex' def create_main(): # creates `main.tex` which only has macro definition latex_exporter = LatexExporter() book = nbformat.v4.new_notebook() book.cells.append( nbformat.v4.new_raw_cell(r'\input{__your_input__here.tex}')) (body, _) = latex_exporter.from_notebook_node(book) with open(MAIN_TEX, 'x') as fout: fout.write(body) print("created:", MAIN_TEX) def init(): create_main() latex_exporter = LatexExporter() # copy `style_ipython.tplx` in `nbconvert.exporters` module to current directory, # and modify it so that it does not contain macro definition tmpl_path = os.path.join( os.path.dirname(exporters.__file__), latex_exporter.default_template_path) src = os.path.join(tmpl_path, 'style_ipython.tplx') target = CUSTOM_TEMPLATE with open(src) as fsrc: with open(target, 'w') as ftarget: for line in fsrc: # replace the line so than it does not contain macro definition if line == "((*- extends 'base.tplx' -*))\n": line = "((*- extends 'document_contents.tplx' -*))\n" ftarget.write(line) print("created:", CUSTOM_TEMPLATE) def group_cells(note): # scan the cell source for tag with regexp `^#latex:(.*)` # if sames tags are found group it to same list pattern = re.compile(r'^#latex:(.*?)$(\n?)', re.M) group = defaultdict(list) for num, cell in enumerate(note.cells): m = pattern.search(cell.source) if m: tag = m.group(1).strip() # remove the line which contains tag cell.source = cell.source[:m.start(0)] + cell.source[m.end(0):] group[tag].append(cell) else: print("tag not found in cell number {}. ignore".format(num + 1)) return group def doit(): with open(sys.argv[1]) as f: note = nbformat.read(f, as_version=4) group = group_cells(note) latex_exporter = LatexExporter() # use the template which does not contain LaTex macro definition latex_exporter.template_file = CUSTOM_TEMPLATE try: os.mkdir(OUTPUT_FILES_DIR) except FileExistsError: pass for (tag, g) in group.items(): book = nbformat.v4.new_notebook() book.cells.extend(g) # unique_key will be prefix of image (body, resources) = latex_exporter.from_notebook_node( book, resources={ 'output_files_dir': OUTPUT_FILES_DIR, 'unique_key': tag }) ofile = tag + '.tex' with open(ofile, 'w') as fout: fout.write(body) print("created:", ofile) # the image data which is embedded as base64 in notebook # will be decoded and returned in `resources`, so write it to file for filename, data in resources.get('outputs', {}).items(): with open(filename, 'wb') as fres: fres.write(data) print("created:", filename) if len(sys.argv) <= 1: print("USAGE: this_script [init|yourfile.ipynb]") elif sys.argv[1] == "init": init() else: doit() 文件中解决此问题。您可能只需要通过重定向到.htaccess来更新它。由于在Angular中,所有内容都在构建后位于index.html中。

index.html

另外,必须像这样<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^(.*) index.html [NC,L] </IfModule> 一样定义所有loadChildren:。因为更新的语法loadChildren: './path'会导致运行时编译器错误。