vue中html、css、js 分離


在正常的創建和引用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>

形式多樣,但根本思想都是分離獨立文件,引入加載

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM