vue中html、css、js 分離

在正常的建立和引用vue文件都是html、css、js三者在一塊兒的,這樣寫起來雖然方便了,可是頁面比較大或者代碼比較多的狀況下,即便使用組件有時代碼也比較多,簡單來講查找不變不利於編程,大的來講影像優化性能。將代碼中的html、css、js分離出來是個很好的選擇。css

首先,在 .vue文件中設置以下:html

<template src="./record.html"></template>
<script src="./record.js"></script>
<style src="./record.scss" lang="scss"></style>

新建index.js文件,以下:vue

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>

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

相關文章
相關標籤/搜索