【问题标题】:if __dirname gets you the folders your in what gets you the root of the project?如果 __dirname 为您提供了您的文件夹,那么您将获得项目的根目录?
【发布时间】:2016-10-12 19:29:30
【问题描述】:

考虑以下项目结构:

game.js我有以下内容:

import React, {Component} from 'react';
import {Loop, Canvas, Sprite}     from '../components/';
import GameLoop           from '../engine/loop';
import path               from 'path';

export default class Game extends Component {

  constructor(props) {
    super(props);
  }

  render() {
    console.log();
    return (
      <Loop>
        <Canvas>
          <Sprite img={'/images/The-Poet.png'} />
        </Canvas>
      </Loop>
    );
  }
}

如果您查看Sprite 我正在尝试传递图像,那么在电子中我如何得到错误:

Failed to load resource: net::ERR_FILE_NOT_FOUND file:///images/The-Poet.png

如果我尝试使用__dirname 它会吐出src/game/... 我需要所述图像的绝对路径。 想法?

【问题讨论】:

    标签: node.js electron absolute-path


    【解决方案1】:

    您可以通过使用 app.getAppPath() 获取当前应用程序目录的路径来构建绝对路径

    http://electron.atom.io/docs/api/app/#appgetapppath

    由于 app 是一个主进程变量,并且您希望在渲染器进程中使用它,因此您必须使用远程来要求它。

    var remote = require('remote')
    var app = remote.require('app')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多