2016-04-16 20 views
0

我正在構建Grunt插件,並且需要選擇Gruntfile.js中的一個目標。獲取Grunt插件的目標

Gruntfile.js:

grunt.initConfig({ 
    plugin: { 
     options: {}, 
     target: {} // <= This is what I want to select 
    } 
}); 

裏面的任務/ plugin.js我可以使用options = this.options()得到options對象,但我不能只用this.target。有什麼想法嗎?

回答

1

Grunt只爲multi-tasks公開this.target。要訪問常規任務相同的值(酒吧grunt foo:bar),訪問args array代替:this.args[0]

更新:

以前的答案是根據由步兵官方術語編寫的。重新閱讀你的問題,你將使用grunt.config()方法access the property within the config object,深入到你試圖在grunt配置對象中訪問的特定屬性。根據你的grunt.initConfig以上,你可以訪問的內容targetgrunt.config('plugin.target');

+0

我使用'console.log(this.args [0])'這給了我'undefined'和'console.log(this .args.length)'給我'0'。 – jasonetco

+0

你用來運行任務的命令 – theaccordance

+0

'grunt.registerTask('default',['plugin']);'< - 那個。我猜你會說要運行'plugin:target',但那不是我想要做的。 – jasonetco