Node.js - 我应该使用history.js的npm安装还是这个包中的单个文件?

时间:2022-05-19 06:31:13

I'm starting a Node.js project using Sails.js and I want to know if I should npm install history.js or should I download and place the js files in my assets linker.

我正在使用Sails.js启动Node.js项目,我想知道我是否应该安装history.js或者我应该下载并将js文件放在我的资源链接器中。

In the npm install I see:

在npm安装中,我看到:

  • /bean
  • /domready
  • history.adapter.ender.js
  • history.js
  • package.json

In the download here I see (for jquery):

在这里下载我看到(对于jquery):

  • history.adapter.jquery.js
  • history.html4.js
  • history.js
  • json2.js

Also, on another page I see:

另外,在另一页上我看到:

  • ajaxify-html5.js

What files should I be using?

我应该使用哪些文件?

1 个解决方案

#1


2  

I think everyone has their own way to do their asset dependencies but I fell in love with doing this in Bower. Here's how I set Bower up to do my assets for my SailsJS apps:

我认为每个人都有自己的方式来做他们的资产依赖,但我爱上了Bower这样做。以下是我如何设置Bower为我的SailsJS应用程序执行我的资产:

First install bower:

首先安装凉亭:

npm install --save bower

Then create a file called .bowerrc which will tell bower where to put your assets:

然后创建一个名为.bowerrc的文件,它会告诉bower将资产放在哪里:

{
    "directory": "assets/components"
}

Then run bower init to initialize your Bower configuration:

然后运行bower init来初始化Bower配置:

bower init
[?] name: myapp
[?] version: 0.1.0
[?] description: my app description
[?] main file:
[?] keywords:
[?] authors: me
[?] license: MIT
[?] homepage:
[?] set currently installed components as dependencies? No
[?] add commonly ignored files to ignore list? Yes
[?] would you like to mark this package as private which prevents it from being accidentally published to the registry? Yes

// ... cut out the resulting JSON ...

[?] Looks good? Yes

What results is a new file called bower.json which contains the JSON configuration. Next you'll want to install history.js which is super simple:

结果是一个名为bower.json的新文件,其中包含JSON配置。接下来,您将要安装history.js,这非常简单:

bower install -S history.js

When you do this it downloads history.js and puts it into your components folder. Since History.JS doesn't require a specific helper library and it sounds like you want to use jQuery you can then install jQuery:

执行此操作时,它会下载history.js并将其放入组件文件夹中。由于History.JS不需要特定的帮助库,听起来你想使用jQuery,你可以安装jQuery:

bower install -S jquery

Next you'll want the linker to use these. So open up Gruntfile.js. Find the section for jsFilesToInject. Add your new components to it:

接下来,您将希望链接器使用这些。所以打开Gruntfile.js。找到jsFilesToInject的部分。将新组件添加到其中:

var jsFilesToInject = [
  'js/socket.io.js',
  'js/sails.io.js',
  'js/app.js',

  // Now the Bower components.
  'components/jquery/jquery.js',

  // I determined this by looking at assets/components/history.js/README.md
  'components/history.js/scripts/bundled/html4+html5/jquery.history.js'
];

Then go and open up views/layout.ejs and add the necessary script and CSS blocks for the linker to work:

然后去打开views / layout.ejs并添加必要的脚本和CSS块以使链接器工作:

<head>
  <!--STYLES-->
  <!--STYLES END-->
  <!--SCRIPTS-->
  <!--SCRIPTS END-->
</head>

These comment tags are pointers for the linker to know where to inject your scripts. Note that the Sails Assets Documentation has more information on configuring the linker, such as additional folders to create for auto-linking your own JS/CSS/JST.

这些注释标记是链接器的指针,用于了解注入脚本的位置。请注意,Sails Assets Documentation提供了有关配置链接器的更多信息,例如要为自动链接自己的JS / CSS / JST而创建的其他文件夹。

Start or restart your Sails app and the linker should copy the files to the appropriate folder and automatically inject them into your layout.

启动或重新启动Sails应用程序,链接器应将文件复制到相应的文件夹并自动将它们注入到您的布局中。

I'd add one more thing. In your package.json for your Node/Sails app I'd also add:

我还要补充一点。在你的Node.json应用程序的package.json中,我还要添加:

{
  "scripts": {
    "postinstall": "./node_modules/bower/bin/bower install"
  }
}

This will trigger npm to run bower install whenever "npm install" is run. So if you deploy your app to a service like Heroku it'll automatically retrieve your assets for you as well.

每当运行“npm install”时,这将触发npm运行bower安装。因此,如果您将应用程序部署到像Heroku这样的服务,它也会自动为您检索资产。

I didn't address Ajaxify and that's because, for whatever reason, it isn't packaged via Bower. You'd have to get this and place it in your assets/js/whatever folder and add it to the linker.

我没有解决Ajaxify问题,因为无论出于何种原因,它都没有通过Bower进行打包。您必须得到它并将其放在您的assets / js / whatever文件夹中并将其添加到链接器中。

You will, also, want to look at its README.md as it says what other dependencies it has and what order to include them in. The order of the JS and CSS files in Gruntfile.js is the order they will be injected into the app. I'd look at the ajaxify docs and add its dependencies via Bower.

您还将要查看其README.md,因为它说明了它具有的其他依赖关系以及包含它们的顺序.Gruntfile.js中JS和CSS文件的顺序是它们将被注入到应用程序。我将查看ajaxify文档并通过Bower添加其依赖项。

Hope this helps.

希望这可以帮助。

#1


2  

I think everyone has their own way to do their asset dependencies but I fell in love with doing this in Bower. Here's how I set Bower up to do my assets for my SailsJS apps:

我认为每个人都有自己的方式来做他们的资产依赖,但我爱上了Bower这样做。以下是我如何设置Bower为我的SailsJS应用程序执行我的资产:

First install bower:

首先安装凉亭:

npm install --save bower

Then create a file called .bowerrc which will tell bower where to put your assets:

然后创建一个名为.bowerrc的文件,它会告诉bower将资产放在哪里:

{
    "directory": "assets/components"
}

Then run bower init to initialize your Bower configuration:

然后运行bower init来初始化Bower配置:

bower init
[?] name: myapp
[?] version: 0.1.0
[?] description: my app description
[?] main file:
[?] keywords:
[?] authors: me
[?] license: MIT
[?] homepage:
[?] set currently installed components as dependencies? No
[?] add commonly ignored files to ignore list? Yes
[?] would you like to mark this package as private which prevents it from being accidentally published to the registry? Yes

// ... cut out the resulting JSON ...

[?] Looks good? Yes

What results is a new file called bower.json which contains the JSON configuration. Next you'll want to install history.js which is super simple:

结果是一个名为bower.json的新文件,其中包含JSON配置。接下来,您将要安装history.js,这非常简单:

bower install -S history.js

When you do this it downloads history.js and puts it into your components folder. Since History.JS doesn't require a specific helper library and it sounds like you want to use jQuery you can then install jQuery:

执行此操作时,它会下载history.js并将其放入组件文件夹中。由于History.JS不需要特定的帮助库,听起来你想使用jQuery,你可以安装jQuery:

bower install -S jquery

Next you'll want the linker to use these. So open up Gruntfile.js. Find the section for jsFilesToInject. Add your new components to it:

接下来,您将希望链接器使用这些。所以打开Gruntfile.js。找到jsFilesToInject的部分。将新组件添加到其中:

var jsFilesToInject = [
  'js/socket.io.js',
  'js/sails.io.js',
  'js/app.js',

  // Now the Bower components.
  'components/jquery/jquery.js',

  // I determined this by looking at assets/components/history.js/README.md
  'components/history.js/scripts/bundled/html4+html5/jquery.history.js'
];

Then go and open up views/layout.ejs and add the necessary script and CSS blocks for the linker to work:

然后去打开views / layout.ejs并添加必要的脚本和CSS块以使链接器工作:

<head>
  <!--STYLES-->
  <!--STYLES END-->
  <!--SCRIPTS-->
  <!--SCRIPTS END-->
</head>

These comment tags are pointers for the linker to know where to inject your scripts. Note that the Sails Assets Documentation has more information on configuring the linker, such as additional folders to create for auto-linking your own JS/CSS/JST.

这些注释标记是链接器的指针,用于了解注入脚本的位置。请注意,Sails Assets Documentation提供了有关配置链接器的更多信息,例如要为自动链接自己的JS / CSS / JST而创建的其他文件夹。

Start or restart your Sails app and the linker should copy the files to the appropriate folder and automatically inject them into your layout.

启动或重新启动Sails应用程序,链接器应将文件复制到相应的文件夹并自动将它们注入到您的布局中。

I'd add one more thing. In your package.json for your Node/Sails app I'd also add:

我还要补充一点。在你的Node.json应用程序的package.json中,我还要添加:

{
  "scripts": {
    "postinstall": "./node_modules/bower/bin/bower install"
  }
}

This will trigger npm to run bower install whenever "npm install" is run. So if you deploy your app to a service like Heroku it'll automatically retrieve your assets for you as well.

每当运行“npm install”时,这将触发npm运行bower安装。因此,如果您将应用程序部署到像Heroku这样的服务,它也会自动为您检索资产。

I didn't address Ajaxify and that's because, for whatever reason, it isn't packaged via Bower. You'd have to get this and place it in your assets/js/whatever folder and add it to the linker.

我没有解决Ajaxify问题,因为无论出于何种原因,它都没有通过Bower进行打包。您必须得到它并将其放在您的assets / js / whatever文件夹中并将其添加到链接器中。

You will, also, want to look at its README.md as it says what other dependencies it has and what order to include them in. The order of the JS and CSS files in Gruntfile.js is the order they will be injected into the app. I'd look at the ajaxify docs and add its dependencies via Bower.

您还将要查看其README.md,因为它说明了它具有的其他依赖关系以及包含它们的顺序.Gruntfile.js中JS和CSS文件的顺序是它们将被注入到应用程序。我将查看ajaxify文档并通过Bower添加其依赖项。

Hope this helps.

希望这可以帮助。