在 vue.config.js 如下设置:
module.exports = {
configureWebpack: {
resolve: {
alias: {
'assets': "@/assets"
}
}
}
}
示例:
<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png" title="Vue--logo">
<img alt="Vue logo" src="~assets/logo.png" title="Vue--logo">
<img alt="Vue logo" :src="require('assets/logo.png')" title="Vue--logo">
<img alt="Vue logo" :src="src1" title="Vue--logo">
<img alt="Vue logo" :src="src2" title="Vue--logo">
<div class="a" title="aaaaaaaa"><p>aaa</p></div>
<HelloWorld msg="Welcome to Your Vue.js App"/>
</div>
</template>
<script>
import HelloWorld from '@/components/HelloWorld.vue'
export default {
name: 'Home',
data() {
return {
src1: require("assets/logo.png"),
src2: require("@/assets/logo.png")
}
},
components: {
HelloWorld
}
}
</script>
<style>
.a {
height: 100px;
background-image: url("~assets/logo.png");
}
</style>
结果图(截了主要的部分):
|