小编Ale*_*dis的帖子

Webpack 导入动态模块数组(在编译时已知)

在我的项目中,我想导入特定的模块,它们的实际路径仅在编译期间才知道。

假设我有Components/A.jsComponents/B.jsComponents/C.js,在我的App.js中,我想包含其中的一个子集,这些子集仅在 webpack 的编译时才知道(例如,来自一个 JSON)

我尝试过的 - 1

//App.js
compileData = await getCompileDataSomehow()

import("./components/" + compileData.component + ".js")
   .then( /* Do stuff with component */);
Run Code Online (Sandbox Code Playgroud)

这项工作很好,但是 webpack 将为每个components/*.js文件创建一个块。此外,还会有额外的网络往返。我觉得这有点矫枉过正,因为当 webpack 运行时我就会知道我想要什么组件。所以我尝试提供组件数组,但没有运气,因为require([...])也被认为是动态的。

我最终得到了什么

//App.js
import("./components/ALL.js") //this file doesn't actually exists
Run Code Online (Sandbox Code Playgroud)
//webpack.config.js

const fs = require('fs')

const componentsToInclude = ["A", "B"] //this will be read from a json


fs.writeFileSync('./src/components/ALL.js',
  `
export default [
    ${componentsToInclude.map(comp => `require("./${comp}.js")` )}
].map(c => …
Run Code Online (Sandbox Code Playgroud)

javascript webpack

7
推荐指数
1
解决办法
672
查看次数

fs.createWriteStream,没有该文件或目录,打开Nodejs

我想将从另一台服务器获取的文件保存在我的服务器上,但问题是当我调用 createWriteStream 时它给了我错误:

没有这样的文件或目录,打开 E:\pathtoproject\myproject\public\profile_14454.jpg

这是我的代码,位于E:\pathtoproject\myproject\modules\dowload.js

request.head(infos.profile_pic, function(err, res, body) {
  const completeFileName = '../public/profile_14454.' + res.headers['content-type'].split('/')[1];
  var imageStream = fs.createWriteStream(completeFileName);
  imageStream.on('open', function(fd) {
    console.log("File open");
    request(infos.profile_pic).pipe(imageStream).on('close', function(body) {
      consoleLog('Profile pic saved');
      console.log('This is the content of body');
      console.log(body);
      connection.query('UPDATE user set photo=? where id=?', [completeFileName, lastID], function(err, result, fields) {
        if (err) {
          consoleLog('Error while update the profile pic');
        }
      });
    })
  });
});
Run Code Online (Sandbox Code Playgroud)

当我删除目录../public/并只保留文件名 profile_14454.' + res.headers['content-type'].split('/')[1]时,它可以工作,但文件保存在项目的根目录中(E:\pathtoproject\myproject\)。

我正在做的事情有什么问题吗?如何将文件保存在公共目录下?

我使用的是nodeJS 8.9.4

node.js

6
推荐指数
1
解决办法
1万
查看次数

在模型类中使用服务

我在项目中为电影创建了一个模型类,其中包含以下内容

//movie/model.ts
export class Movie {

  title:string;
  overview:string;
  id:string;
  seasons:any[];

  constructor(obj: any) {

        this.id = obj.id;
        this.overview = obj.overview;
        this.title = obj.title;

    }
}
Run Code Online (Sandbox Code Playgroud)

此外,我已经提供了一个服务,使所有的http调用从api获取数据.

//api.service.ts
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/Rx';

import {Movie} from './movie/model';

@Injectable()
export class ApiService {

  constructor(private http: Http) {}

  getMovie(id):{

    let url = this.Api_Url + '/movies/' + id + "?extended=full,images";

    return this.http.get(url, { headers: this.headers })
      .map(d => d.json())
      .map(d => new Movie(d))
      .toPromise()
  } …
Run Code Online (Sandbox Code Playgroud)

javascript angular

5
推荐指数
1
解决办法
489
查看次数

标签 统计

javascript ×2

angular ×1

node.js ×1

webpack ×1