vue动态引入静态资源

vue动态引入静态资源

静态资源位置( ../../assets/piecture/page404.jpg**)或者(** @/assets/piecture/page404.jpg**)**

错误引入方式

错误引入方式(一)

html 复制代码
<template>
  <div>
    <img :src="`../../assets/piecture/${page404_$}.jpg`" class="page-404" />
  </div>
</template>

<script>
export default {
  name: "NotFound",
  data() {
    return {
      page404_$: "page404"
    };
  }
};
</script>

错误引入方式(二)

html 复制代码
<template>
  <div>
    <img :src="page404Path" class="page-404" />
  </div>
</template>

<script>
export default {
  name: "NotFound",
  data() {
    return {
      page404Path: "../../assets/piecture/page404.jpg",
    };
  }
};
</script>

正确引入方式

正确引入方式(一)

javascript 复制代码
<template>
  <div>
    <img src="../../assets/piecture/page404.jpg" class="page-404" />
  </div>
</template>

<script>
export default {
};
</script>

正确引入方式(二)

html 复制代码
<template>
  <div>
    <img :src="page404Imply" class="page-404" />
  </div>
</template>

<script>
import page404Imply from "@/assets/piecture/page404.jpg";
export default {
  name: "NotFound",
  data() {
    return {
      page404Imply
    };
  }
};
</script>

正确引入方式(三)

html 复制代码
<template>
  <div>
    <img :src="page404" class="page-404" />
  </div>
</template>

<script>
export default {
  name: "NotFound",
  data() {
    return {
      page404: require("@/assets/piecture/page404.jpg"),
    };
  }
};
</script>

正确引入方式(四)

html 复制代码
<template>
  <div>
    <img :src="page404ImpAsync" class="page-404" />
  </div>
</template>

<script>
export default {
  name: "NotFound",
  data() {
    return {
      page404ImpAsync: "",
    };
  },
  created() {
    import("@/assets/piecture/page404.jpg").then((res) => {
      this.page404ImpAsync = res.default;
    });
  },
};
</script>
相关推荐
小锋java12345 分钟前
【技术专题】Vue3 - 生命周期
vue.js·vite
夏幻灵1 小时前
从语法坑点到 JS 内存底层:Vue 3 watch 侦听器深度全解
开发语言·javascript·vue.js
Sterting2 小时前
反馈组件:对话框、消息与通知
前端·javascript·vue.js
书中枫叶2 小时前
做了个「句拾」小程序,最难的不是业务,是字体
前端·javascript·vue.js
新时代牛马3 小时前
Vue.js 响应式原理详解
前端·javascript·vue.js
满栀5854 小时前
vue3动态路由详细效果
前端·javascript·vue.js·typescript·前端框架
山荷枝5 小时前
05-Vue
前端·javascript·vue.js
可爱的秋秋啊6 小时前
vue调用腾讯人脸组件封装+接口请求后端调用
前端·javascript·vue.js
夏幻灵20 小时前
Vue 2 与 Vue 3 在应用初始化上的设计差异与技术演进
前端·javascript·vue.js
Sterting1 天前
Form表单与校验
javascript·vue.js·elementui