简体   繁体   English

如何使用 javascript 中的 vscode (visual studio) 通过智能感知从一个文件访问定义的函数到另一个文件?

[英]How to access functions defined from one file to other via intellisense with vscode (visual studio) in javascript?

Consider folder structure as below in javascript project built-in Visual Studio 1.43在 javascript 项目内置 Visual Studio 1.43 中考虑如下文件夹结构

/Folder1
   /testFunctions1.js
       /function1()
       /function2()
       ...

/Folder2
   /testFunctions2.js
      require('testFunctions1.js') //to include 

      functio //Here If I press ctrl+space, I expect suggestions of functions defined in testFunctions1.js

How do I achieve it in java-script?如何在 java 脚本中实现它?

pre-reqs: javascript/Visual Studi 1.43 (2020)先决条件:javascript/Visual Studi 1.43 (2020)

Update 1:更新 1:

../Folder1/testFunctions1.js
test1 : function()
{
    ..
    ..
}

test2 : function()
{
    ..
    ..
}

../Folder2/testFunctions2.js
require('../Folder1/testFunctions1.js')

test (ctrl+space) //expect suggestions here

Update 2:更新 2:

   function doSomething1() {
    ..
    }

    function doSomething2() {
    ..
    }

If I include above with reference, I get suggestions - It works.如果我在上面包含参考,我会得到建议 - 它有效。

But If I have functions in below format then, it fails.但是,如果我有以下格式的功能,那么它会失败。 I want doSomething in suggestions.我想在建议中做一些事情。

//testFunctions1.js
define('../mypageobjects/PageObject.js', 
[
   '../test/tests.js'
], 

function (PageObject,commonLibWidget){
    var test = PageObject.extend('test', {       
    check: function () {
        return true;
    },

    selectors: {
        //home page
        widgetHeader: '.moduleHeader_tle',
    },

    commands: {

        doSomething1: function(){ //I want this function name - doSomething1
            return this
        },

         doSomething2: function(){ //I want this function name - doSomething2
            return this
        },     

    }  //commands close

    }); //pageobject.extend
    return test;
}); //main function

Try adding the following comment to the top of your testFunctions2.js file:尝试将以下注释添加到testFunctions2.js文件的顶部:

/// <reference path="../Folder1/testFunctions1.js" />

示例 - vscode 在行动

This should work in Visual Studio and VSCode.这应该适用于 Visual Studio 和 VSCode。

See also: What is reference path in vscode另请参阅: 什么是 vscode 中的引用路径

I know this is a old question, but I got to the same problem.我知道这是一个老问题,但我遇到了同样的问题。

I think that there is a feature now related to the file jsconfig.json .我认为现在有一个与文件jsconfig.json相关的功能。

Putting this file in the root folder, I could get the intellisense for functions and objects methods, look these examples below.将此文件放在根文件夹中,我可以获得函数和对象方法的智能感知,请看下面的这些示例。

(note: in the case of object methods, the object needs to be define with the let statement) (注意:对于 object 方法,object 需要用let语句定义)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM