Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将请求参数传递给Angular应用程序?_Javascript_Angular_Express - Fatal编程技术网

Javascript 如何将请求参数传递给Angular应用程序?

Javascript 如何将请求参数传递给Angular应用程序?,javascript,angular,express,Javascript,Angular,Express,我有一个Angular应用程序(由Angular CLI构建),它通过Heroku上的express托管: const express = require('express'); const app = express(); // Run the app by serving the static files // in the dist directory app.use(express.static(__dirname + '/dist')); // Start the app by lis

我有一个Angular应用程序(由Angular CLI构建),它通过Heroku上的
express
托管:

const express = require('express');
const app = express();
// Run the app by serving the static files
// in the dist directory
app.use(express.static(__dirname + '/dist'));
// Start the app by listening on the default
// Heroku port
app.listen(process.env.PORT || 8080);
此应用程序是一个位于
vk.com
的iframe,其中用户的浏览器将请求中的字符串传递给托管我的应用程序的服务器,其中包含一些重要信息,如用户id等。看起来是这样的(实际值替换为星号):


在应用程序进入用户浏览器并加载之前,是否有任何方法提取这些值并将其传递给应用程序?因此,它们可以存储在变量或类似的东西中。

您可以像javascript一样将位置值存储到javascript中。您可以将此代码放在根组件中
getParameterName
函数是从上述链接复制的

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,
})
export class App {
  name:string;
  constructor() {
    this.name = `Angular! v${VERSION.full}`+getParameterByName('name');
  }
}
函数getParameterByName(名称、url){ 如果(!url)url=window.location.href; name=name.replace(/[\[\]]/g,\\$&); var regex=new RegExp(“[?&]”+name+”(=([^&#]*)和|#|$), 结果=regex.exec(url); 如果(!results)返回null; 如果(!results[2])返回“”; 返回组件(结果[2]。替换(/\+/g,”); } @组成部分({ 选择器:“我的应用程序”, 模板:` 你好{{name} `, }) 导出类应用程序{ 名称:字符串; 构造函数(){ this.name=`Angular!v${VERSION.full}`+getParameterByName('name'); } } 我在这里试过,效果很好。

从您的措辞中不确定是浏览器请求还是服务器响应。很难回答。模块@angular/http具有请求、响应、头、请求选项、URLSearchParams等功能。在Angular 2或Angular 4的上下文中使用谷歌搜索这些名称。当用户在
vk.com
打开一个带有iframe的页面,其中包含我的应用程序时,他的浏览器会向我的托管应用程序发出请求,这是一个带有
express
的javascript。在该脚本中,我可以从原始请求中获取所有参数,然后我需要将它们传递给通过
express
托管的实际Angular应用程序。
function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,
})
export class App {
  name:string;
  constructor() {
    this.name = `Angular! v${VERSION.full}`+getParameterByName('name');
  }
}