When we were learning HTML, the image tag introduced the image
<img src=".. /assets/images/avatar.png" width="100%">
Copy the code
But there are two drawbacks:
- Because absolute path import is used, if the following image moves the directory, you need to change the path in the SRC generation
- If the image is used in more than one place on the same page, it will need to be imported multiple times, and the image has moved the directory so that the SRC path needs to be changed in so many places
How to do? Use dynamic paths import, require
Before ES6, JS didn’t have its own module syntax. To address this embarrassment, require.js was created. After ES6, JS introduced the concept of import
- use
import
The introduction of
Import needs to be registered in data, otherwise it will not display
<script>
import lf1 from '@/assets/images/cityOfVitality/lf1.png'
import lf2 from '@/assets/images/cityOfVitality/lf2.png'
import lf3 from '@/assets/images/cityOfVitality/lf3.png'
import lf4 from '@/assets/images/cityOfVitality/lf4.png'
import lf5 from '@/assets/images/cityOfVitality/lf5.png'
import lf6 from '@/assets/images/cityOfVitality/lf6.png'
import lf7 from '@/assets/images/cityOfVitality/lf7.png'
import top1 from '@/assets/images/cityOfVitality/icon_top1.png'
import mixins from './mixins'
export default {
name: 'LeftPiece'.mixins: [mixins],
data () {
return {
lf1,
lf2,
lf3,
lf4,
lf5,
lf6,
lf7,
top1
}
}
}
</script>
Copy the code
- use
require
The introduction of
<script>
import top1 from '@/assets/images/cityOfVitality/icon_top1.png'
import mixins from './mixins'
export default {
name: 'RightPiecr'.mixins: [mixins],
data () {
return {
rt1: require('@/assets/images/cityOfVitality/rt1.png'),
rt2: require('@/assets/images/cityOfVitality/rt2.png'),
rt3: require('@/assets/images/cityOfVitality/rt3.png'),
rt4: require('@/assets/images/cityOfVitality/rt4.png'),
rt5: require('@/assets/images/cityOfVitality/rt5.png'),
rt6: require('@/assets/images/cityOfVitality/rt6.png'),
top1
}
}
}
</script>
Copy the code