Home  >  Q&A  >  body text

javascript - webpack compiles the content in the backtick but reports an error, but I write the string directly and no error is reported

const path = './../views/'
export default new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: resolve => require([`${path}index`], resolve)
    }
  ]  

The above writing method will report an error

Error: Cannot find module './../views/index'.
    at webpackContextResolve (eval at 126 (10.js:6), <anonymous>:10:9)
    at webpackContext (eval at 126 (10.js:6), <anonymous>:5:29)
    at eval (eval at <anonymous> (app.js:962), <anonymous>:16:132)

But I changed it to component: resolve => require(['./../views/index'], resolve)No error will be reported, but in fact it is the same, please give an expert answer Why?

PHP中文网PHP中文网2732 days ago825

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:30:09

    The variables are in backticks... they are static in single quotes. This is the difference. Dynamic writing is not supported here

    reply
    0
  • 为情所困

    为情所困2017-05-19 10:30:09

    Related to nodejs. Because there are two path forms for nodejs file import, one is to point to a js file, and the other is to point to a folder. When it is a folder, the index.js file in the folder will be referenced by default. Your path here is a folder. Since you don’t have an index.js file in it, an error will be reported. Of course you can also customize the file. You can create a new package.json file in this folder and write an object in it { main: "Write the name of the file you need to export by default" };

    reply
    0
  • 高洛峰

    高洛峰2017-05-19 10:30:09

    Has the question been solved

    reply
    0
  • Cancelreply