在vue-cli中引入css有多種方式,以下是一些常見的方法:
1、全局引入CSS:
在vue.config.js
文件中,可以通過css
字段引入全局CSS文件。
```javascript
module.exports = {
css: [
'path/to/your/css/file.css'
]
}
```
這樣,整個應用都會使用這個CSS文件。
2、組件內(nèi)引入CSS:
在Vue組件中,可以使用<style>
標簽引入CSS。
```vue
<template>
<div class="my-component">Hello World!</div>
</template>
<style>
.my-component {
color: red;
}
</style>
```
這種方式只會影響當前組件的樣式。
3、使用CSS模塊:
Vue支持CSS模塊,可以將CSS文件作為模塊導入。
```javascript
import myCSS from 'path/to/your/css/file.css';
```
在組件中使用這個CSS模塊:
```vue
<template>
<div class="my-component">Hello World!</div>
</template>
<script>
import myCSS from 'path/to/your/css/file.css';
export default {
name: 'MyComponent',
css: myCSS,
}
</script>
```
這種方式可以將CSS文件作為模塊導入,并在組件中使用。
4、使用樣式綁定:
Vue還支持使用樣式綁定來動態(tài)設置樣式。
```vue
<template>
<div :style="{ color: dynamicColor }">Hello World!</div>
</template>
<script>
export default {
name: 'MyComponent',
data() {
return { dynamicColor: 'red' };
}
}
</script>
```
這種方式可以動態(tài)地根據(jù)數(shù)據(jù)或條件來設置樣式。