全局声明的变量在其他模块中不可见

时间:2016-03-10 03:17:04

标签: node.js global-variables

我正在声明全局变量如下:

ReferenceError: NODE_PATH is not defined
    at Object.<anonymous> (z:\$app4pc\website_engine\conf\dbconnection.js:1:83)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (z:\$app4pc\website_engine\models\login_model.js:1:76)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)

Process finished with exit code 1

但是当我在其他模块中访问它时,例如flollows:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String message = intent.getStringExtra (MESSAGE_KEY);
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText (message);
        setContentView(textView);
        setContentView(R.layout.activity_fire_alert);
    }

它出现以下错误:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.user.tictactoeniandy.FireAlertActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="View Sender Location"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="91dp"
        android:id="@+id/loc_button1"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

Node.js不会自动将事物放在全局范围内。您应该从模块中导出它,并在使用它的模块中要求它。如果必须设置全局变量,则可以使用:

var GLOBAL.NODE_MODULES_PATH = ...