nuxt 项目引入自定义svg图标和iconfont.js

引入iconfont.js

  1. 因为iconfont相当于插件了,所以要在nuxt.config.js中的plugins中进行注入
plugins: [{ src: "./static/font/iconfont.js", ssr: false }]

ssr要使用false,因为iconfont.js中有用到window,在服务端渲染时会报错

不要在组件中引用,在nuxt中要当成插件引入

引入自定义svg

  1. 创建svg组件icon-svg
<template>
  <svg :class="svgClass" :style="[baseStyles, addStyles]" @click="onClick" aria-hidden="true">
    <use :xlink:href="iconName" />
  </svg>
</template>
<script>
export default {
  name: 'IconSvg',
  props: {
    iconClass: {
      type: String,
      required: true
    },
    styleObj: {
      type: Object,
      default() {
        return {}
      }
    },
    width: {
      type: [String, Number],
      default: '10'
    },
    height: {
      type: [String, Number],
      default: '10'
    },
    color: {
      type: String,
      default: ''
    },
    addStyles: {
      type: Object,
      default() {
        return {}
      }
    },
    className: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      baseStyles: {
        width: this.width + 'px',
        height: this.height + 'px',
        color: this.color
      }
    }
  },
  computed: {
    iconName() {
      return `#icon${this.iconClass}`
    },
    svgClass() {
      if (this.className) {
        return 'svg-icon ' + this.className
      } else {
        return 'svg-icon'
      }
    }
  },
  methods: {
    onClick() {
      this.$emit('Click')
    }
  }
}
</script>
<style scoped lang="less">
  .svg-icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    fill: @mainColor;
    overflow: hidden;
  }
</style>
  1. 我们都知道,在使用nuxt的时候,我们使用任何插件的时候都需要在plugins文件夹创建,所以,我们在这里创建icon.js文件,为了去创建一个全局的Icon组件,代码如下:
import Vue from 'vue'

import IconSvg from '_c/common/icon-svg'
Vue.component('icon-svg', IconSvg)
const req = require.context('@/icons/svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)

const req = require.context('@/icons/svg', false, /\.svg$/)
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)

这段代码必须要引入,否则打包后自定义的svg图标不会显示
3. 在nuxt.config.js中的Plugin注入

plugins: [ '@/plugins/icon'],
  1. 配置nuxt.config.js中的build
 /*
   ** Build configuration
   */
  build: {
    /*
     ** You can extend webpack config here
     */
    extend(config, ctx) {
      const svgRule = config.module.rules.find(rule => rule.test.test('.svg'))
      svgRule.exclude = [resolve('icons/svg')]

      // Includes /assets/icons/svg for svg-sprite-loader
      config.module.rules.push({
        test: /\.svg$/,
        include: [resolve('icons/svg')],
        loader: 'svg-sprite-loader',
        options: {
          symbolId: 'icon-[name]',
        },
      })
    },
  },
  1. npm i svg-sprite-loader -S安装svg-sprite-loader依赖
  2. 项目中使用<icon-svg icon-class="pumkin-collected" width="16" height="14" />
要在nuxt.js中使用three.js,可以按照以下步骤操作: 1. 安装three.js 可以通过npm安装three.js: ``` npm install three ``` 2. 创建一个Vue组件 在nuxt.js中,所有页面都被组织成Vue组件。要使用three.js,需要在一个组件中引入它。 可以创建一个名为`MyThreeComponent.vue`的组件,并在其中引入three.js: ```html <template> <div ref="container"></div> </template> <script> import * as THREE from 'three'; export default { mounted() { this.init(); }, methods: { init() { const container = this.$refs.container; // 创建场景 const scene = new THREE.Scene(); // 创建相机 const camera = new THREE.PerspectiveCamera( 45, container.clientWidth / container.clientHeight, 1, 1000 ); camera.position.set(0, 0, 10); // 创建渲染器 const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(container.clientWidth, container.clientHeight); container.appendChild(renderer.domElement); // 创建一个立方体 const geometry = new THREE.BoxGeometry(1, 1, 1); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geometry, material); scene.add(cube); // 渲染场景 renderer.render(scene, camera); }, }, }; </script> ``` 在这个组件中,我们创建了一个场景、一个相机、一个渲染器和一个立方体,并将它们渲染到页面上。 3. 在页面中使用组件 现在,我们可以在任何一个页面中使用这个组件了。例如,在`pages/index.vue`中,可以这样使用: ```html <template> <div> <MyThreeComponent /> </div> </template> <script> import MyThreeComponent from '~/components/MyThreeComponent.vue'; export default { components: { MyThreeComponent, }, }; </script> ``` 在这个页面中,我们引入了`MyThreeComponent`组件,并将它渲染到页面上。 现在,当我们访问这个页面时,就会看到一个绿色的立方体在屏幕中心。 这就是在nuxt.js中使用three.js的基本步骤。当然,你可以根据自己的需求进行更多的修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值