在正常的創建和引用vue文件都是html、css、js三者在一起的,這樣寫起來雖然方便了,但是頁面比較大或者代碼比較多的情況下,即使使用組件有時代碼也比較多,簡單來說查找不變不利於編程,大的來說影像優化性能。將代碼中的html、css、js分離出來是個很好的選擇。
首先,在 .vue文件中設置如下:
<template src="./record.html"></template> <script src="./record.js"></script> <style src="./record.scss" lang="scss"></style>
新建index.js文件,如下:
import record from './record.vue'; export default record;
建立相對應的record.html、record.js、record.scss的文件,就可以了,拿.js來說:
// -- NAME -- const name = 'record'; // -- DATA -- const data = function () { return { }; }; // -- COMPUTED -- const computed = { }; // -- COMPONENTS -- const components = { } // -- WATCH -- const watch = { }; // -- METHODS -- const methods = { }; // -- HOOKS -- function mounted() { } // == EXPORT == export default { name: name, data: data, components: components, computed: computed, watch: watch, methods: methods, mounted: mounted };
另一種方法可以直接引用:
<template> <div>html</div> </template> <script src="./***.js"></script> <style src="./***.css"></style>
形式多樣,但根本思想都是分離獨立文件,引入加載