sfsoul / personalBlog

思考与总结
MIT License
1 stars 0 forks source link

Vue CLI 搭建项目实现上传图片 #16

Open sfsoul opened 4 years ago

sfsoul commented 4 years ago

按需引入 Element UI

借助 babel-plugin-component 可实现按需引入,来达到减小项目体积的目的。

npm i babel-plugin-component -D

修改 .babelrc 文件:

{
  "presets": [["es2015", { "modules": false }]],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}
sfsoul commented 4 years ago

Error: Cannot find module 'babel-preset-es2015' from '/Users/jing_zhang/Desktop/picture-bed'

sfsoul commented 4 years ago

图片上传 === 服务端

sfsoul commented 4 years ago

Node 端设置支持跨域

sfsoul commented 4 years ago

axios 设置拦截器

// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  });
sfsoul commented 4 years ago

Node 访问本地静态资源

const express = require('express');
const app = express();

// 静态资源
app.use(express.static('public'));

// 起服务
app.listen(8888, () => {
    console.log('listening on port 8888')
})

文件目录

预览

sfsoul commented 4 years ago

multer

sfsoul commented 4 years ago

参考文章