How to load external html file in a template in Vu

2019-06-15 18:53发布

I'm new to vue js. I'm just creating a simple project where I just include vuejs through CDN. not using node/npm or cli.

I keep all my html markup in single html which looks messy as it grows. I tried to split html to views and want to include it by something analogous to ng-include of angularJs

I have worked in angular previously where there is ng-include to load external html files. I'm looking for something similar to that in vue. the whole point is to split my html files into more maintainable separate files.

have come across <template src="./myfile.html"/> but it doesn't work Can somebody help me out

3条回答
老娘就宠你
2楼-- · 2019-06-15 19:04

It's actually remarkably easy, but you need to keep something in mind. Behind the scenes, Vue converts your html template markup to code. That is, each element you see defined as HTML, gets converted to a javascript directive to create an element. The template is a convenience, so the single-file-component (vue file) is not something you'll be able to do without compiling with something like webpack. Instead, you'll need to use some other way of templating. Luckily there are other ways of defining templates that don't require pre-compiling and are useable in this scenario.

1 - string/template literals

example: template: '<div>{{myvar}}</div>'

2 - render function

查看更多
何必那么认真
3楼-- · 2019-06-15 19:12

You cant. You must use async components - read guide here

查看更多
三岁会撩人
4楼-- · 2019-06-15 19:21

ng-include was really awesome for loading templates for router. An alternative is using fetch and implementing your own promise.

查看更多
登录 后发表回答