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>
相关推荐
码喽7号1 天前
Vue学习七:MockJs前端数据模拟
前端·vue.js·学习
RONIN1 天前
VUE开发环境配置基础(构建工具→单文件组件SFC→css预处理器sass→eslint)及安装脚手架
vue.js
RONIN1 天前
vue2、vue3区别之混入mixins和过滤器filter
vue.js
RONIN1 天前
属性透传attribute、vue实例对象方法$nextTick()、虚拟dom与浏览器渲染机制
vue.js
RONIN1 天前
vue自定义指令与自定义插件
vue.js
RONIN1 天前
属性透传attribute与性能优化组件(component、异步组件、keep-alive/Suspense/Teleport/Transition)
vue.js
RONIN1 天前
组件通讯(父传子、子传父、ref属性、表单双向绑定v-model、兄弟间传值Event Bus、插槽、依赖注入)
vue.js
RONIN1 天前
vue组件、组件生命周期、组件分离模块化
前端·vue.js
RONIN1 天前
vue开发环境与基础语法、计算属性、侦听属性
前端·vue.js
只会写Bug1 天前
后台管理项目中关于新增、编辑弹框使用的另一种展示形式
前端·vue.js