簡體   English   中英

如何構建Meteor應用程序並將其加載到Meteor Shell中

[英]How to structure Meteor app and load into Meteor shell

在學習Meteor時,我遇到了很多問題,它們構成了非常簡單的代碼。 請參閱注釋,這些都是問題。

服務器/ main.js

import { Meteor } from 'meteor/meteor';

import { Post } from './schema'
// Why is this required to make Post available in Meteor.startup?
// Isn't there auto-loading?

Meteor.startup(() => {
    console.log(Post)
    // This works, but why isn't Post available to meteor shell?
});

服務器/schema.js

import { Post } from './models/post'
export { Post }

服務器/模型/ post.js

import { Class } from 'meteor/jagi:astronomy';
// Why can't this be imported elsewhere, like main.js?

const Posts = new Mongo.Collection('posts');
const Post = Class.create({
  name: 'Post',
  collection: Posts,
  fields: {
    title: { type: String },
    userId: String,
    publishedAt: Date
  },
});

export { Post }

除了這些問題,如何將我的應用程序加載到流星外殼中? Post是不確定的存在,即使在已定義Meteor.startup 我嘗試將.load與絕對路徑一起使用,但這會中斷我的應用程序的導入,該導入使用相對路徑。

至於我對哪些錯誤感到困惑:

  • 當我嘗試在Meteor.startup()使用import ,出現一個錯誤,指出關鍵字import未定義。 我正在使用ecmascript包。
  • 當我沒有在使用Class的同一文件中import { Class }時,出現未知的關鍵字錯誤。
  • 如果我沒有在main.js中import { Post } ,則Post是未定義的。
  • 無法將應用程序加載到Meteor Shell中。

要訪問流星外殼中的導出對象,請使用require

> require('server/schema.js').Posts.findOne();

要訪問包導出的對象,請使用包名稱:

> require('react').PropTypes;

您無法訪問由另一個js文件導入的對象的原因是,每個文件在這里都有自己的作用域。 當Meteor構建您的應用程序時,它不僅像許多其他構建系統一樣連接js文件,這確實是一件好事。

基本上,將為您編寫的每個js文件創建一個Javascript對象。 您在js文件中導出的所有內容都將成為此對象中的字段,您可以使用require訪問。 import只是同一件事的一個更好的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM