问题描述
使用express构建的服务,没有使用视图引擎,将项目目录设置为静态:
app.use(express.static(__dirname + ’/public’));app.use(express.static(__dirname + ’/view’));app.use(express.static(__dirname + ’/node_modules’));
node入口文件index.js在根目录下,目录结构为:
//index.js/public/view/view/index.html(首页)/view/sidebar.html(侧边菜单)/public/js/app.js(首页的js文件)
index.html中引入如下:
<script src='https://www.6hehe.com/wenda/angular/angular.min.js'></script><script src='https://www.6hehe.com/wenda/angular-route/angular-route.min.js'></script><script src='https://www.6hehe.com/wenda/js/app.js'></script>
index.html指令如下:
<my-sidebar></my-sidebar>
app.js如下:
var app = angular.module('myBlog', ['ngRoute']);app.directive('mySidebar', function () { return {restrict: 'E',replace: true,templateURL: 'sidebar.html', }})
经过测试发现templateURL没有起作用,请问应该如何写这个路径,或者如何查看这里的templateURL到底被解析成了什么样子?
问题解答
回答1:已经解决,我的chrome出了点问题,原代码即是正确写法。提供一种使用ejs视图引擎的方法:
app.engine(’html’, require(’ejs’).__express);app.set(’view engine’, ’html’);app.get(’/sidebar.html’, function (req, res) { res.render(’sidebar.html’);})回答2:
../../view/sidebar.html
回答3:把templateURL写成绝对路径 templateURL: ’/view/sidebar.html’
或者在<base>标签里加上组件html模板的根路径