小编Rah*_*ore的帖子

从存储在变量中的 React 元素获取文本内容

有没有办法从存储在变量中的 React 元素中获取文本内容而无需引用?

有一个功能组件,它接收titleprop,其中包含 React 元素:

function component({ title }) {
 const content = title.textContent() // Need Something like this
}
Run Code Online (Sandbox Code Playgroud)

这个title道具可能有像这样的反应节点:<div>Some Title</div>。但我只想在渲染之前获取变量中节点的内容。是否可以?

当我 console.logtitle变量时,这是输出,我想要的内容在props.children数组内部,那么有没有一种方法可以在不遍历键的情况下获取它:

在此输入图像描述

javascript reactjs

9
推荐指数
1
解决办法
1万
查看次数

无法在Node.js res.body中获取$ http.post请求数据

我正在为项目使用Angular 1.3和node.js 0.12.2.我正在使用node.js api

$http.post("url", request_data){}
Run Code Online (Sandbox Code Playgroud)

在服务器端使用这个:

console.log(req.body)
Run Code Online (Sandbox Code Playgroud)

但每次的API被调用时,它得空对象{}request_data,无法获取的问题是什么.我用过body_parser这样的:

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
Run Code Online (Sandbox Code Playgroud)

还尝试在angular $ http中添加内容类型标头:

headers : {'Content-Type': 'applicatio n/x-www-form-urlencoded'}
Run Code Online (Sandbox Code Playgroud)

但没有得到请求数据.

编辑:

Node.js代码:

router.post('/url',function(req,res){
    console.log(req.body)  
})
Run Code Online (Sandbox Code Playgroud)

注意:Developer Tool的网络选项卡显示正在发送的请求标头中的数据,但node.js服务器未接收到req.body.在POSTman中获取数据是正确的响应.

json http-post node.js angularjs

8
推荐指数
1
解决办法
4367
查看次数

材料设计精简版,检查滚动是否到达底部

我正在使用Material Design lite和Angular.js.在事件到达底部时遇到问题以调用事件.我几乎尝试了所有解决方案但没有工作.像jQuery解决方案:

$(window).scroll(function() {
    if($(window).scrollTop() + $(window).height() ==      
        $(document).height())        
    {
        alert("bottom!");
    }
});
Run Code Online (Sandbox Code Playgroud)

或Javascript一:

document.addEventListener('scroll', function (event) {
    if (document.body.scrollHeight == 
        document.body.scrollTop + window.innerHeight) {
        alert("Bottom!");
    }
});
Run Code Online (Sandbox Code Playgroud)

没有工作.根据这个问题:材质设计和jQuery,滚动不起作用

滚动事件将在.mdl-layout__content类上触发,但仍无法获取底部到达的事件.

也许我不想在"mdl-layout__content"类上绑定偶数,因为这将包含在每个页面上.我只想把它放在一个特定的页面上.

编辑: 此代码工作正常,但有问题:

function getDocHeight() {
     var D = document;
     return Math.max(
            document.getElementById("porduct-page-wrapper").scrollHeight,
            document.getElementById("porduct-page-wrapper").offsetHeight,
            document.getElementById("porduct-page-wrapper").clientHeight
        );
}
window.addEventListener('scroll', function(){
    if($('.mdl-layout__content').scrollTop() + $('.mdl-layout__content').height() == getDocHeight()) {
           alert("bottom!");
    }
}, true);
Run Code Online (Sandbox Code Playgroud)

问题是,每次我更改页面并返回时,事件应该调用的次数是相乘的.每当我更改页面或点击链接时,它可能会一次又一次地发生绑定事件.我正在使用Angular.js,我该如何防止这种情况?

javascript jquery scroll angularjs material-design-lite

6
推荐指数
1
解决办法
817
查看次数

浏览器后退按钮更改动态URL参数

我正在一个网站上工作,其中url参数根据用户操作进行更新,根据该网站我更新网页而不刷新.考虑电子商务的场景,当用户点击过滤器然后显示更新的产品时,网址会发生变化.

现在的问题是,当用户点击浏览器的后退按钮时,浏览器会返回上一个url-parameter,但页面没有被更改.我想根据点击后退按钮后更改的url参数更改页面.

我试过这个解决方案:

$($window).on('popstate', function (e) {
     // Update the page also
});
Run Code Online (Sandbox Code Playgroud)

这段代码的问题是,当url发生更改时会触发此问题,这意味着它不关心是否单击了浏览器后退按钮,或者是否使用jQuery更改了url.因此,如果我根据用户交互更改URL,则将调用popstate回调并调用我的自定义函数.要更新页面,我使用的是https请求,因此http api会被调用两次.

有没有办法检查是否只点击了"后退按钮"?

javascript jquery back-button url-parameters angularjs

6
推荐指数
1
解决办法
887
查看次数

$ state.go不会更改Url

我正在使用Angular.js 1.3,使用ui-router.我有3页,page1.html,page2.html,page3.html.

当用户点击page1时,page2将打开,但我想保存第1页的滚动状态,用户在点击之前,所以在点击后退按钮后,他将处于相同的滚动状态.

为了解决这个问题,我在page1.html上打开了page2.html,在iframe中,并给它绝对位置显示在page1.html上,我使用的是:

history.pushState({}, '', '/page2.html');

改变网址.这个实现工作正常.

现在,当用户点击page2.html上的链接时,它应该打开page3.html,就像我使用的普通链接一样:

$state.go("page3")
Run Code Online (Sandbox Code Playgroud)

问题是现在状态变化,并且page3.html加载,但url仍然是/page2.html,url没有变化.

我甚至尝试过:

history.pushState({}, '', '/page3.html');
Run Code Online (Sandbox Code Playgroud)

仍然网址没有变化.任何人都知道它为什么会发生.

javascript url browser-history pushstate angularjs

6
推荐指数
1
解决办法
280
查看次数

git:无法克隆私有存储库,错误:找不到存储库

我的一位朋友加入了他作为合作者的Github项目.我能够看到回购,但当我尝试克隆时:

git clone https:/github.com/FriendsUserName/FriendsRepo.git
Run Code Online (Sandbox Code Playgroud)

获取错误:

remote: Repository not found.
fatal: repository https:/github.com/FriendsUserName/FriendsRepo.git not found
Run Code Online (Sandbox Code Playgroud)

我已经成功设置了多个Github帐户,从这篇文章 - Github工作和娱乐(多个帐户)

以前有人遇到过这个问题吗?

我也尝试分叉回购然后克隆.但仍然与克隆相同的错误.

git github repository git-clone

6
推荐指数
1
解决办法
1775
查看次数

Angular UI-Select为"标记"对象添加重复标记

我正在使用ui-select库来进行"标记"功能.

我正在使用对象数组,其中每个对象都有id和name.它工作正常.

如果我输入不存在的标签,它会创建一个我想要的新标签,但我唯一的问题是如果用户输入已经存在的标签,它会同时显示新标签和现有标签.ui-select只有在尚未存在时才允许新标记.

在此输入图像描述

如果我输入算法,那么它应该选择/显示现有的"Algorithm"标签,而不是允许重复标签插入.

我无法找到任何设置.他们的标记示例页面ui-select标记示例也发生了同样的问题.我想这不是那样的.那么在ui-select中这是可能的,还是应该在我的代码中处理它?有解决方案吗

这是我的代码:

<ui-select multiple tagging="questionVm.addNewTag" theme="select2" ng-model="addQueVm.newQuestion.tags" class="mdl-select">
    <ui-select-match  placeholder="Enter tags">
        <span ng-bind="$item.name"></span>
    </ui-select-match>
    <ui-select-choices repeat="tag in (questionVm.allTags | filter: $select.search)">
        <span ng-bind="tag.name"></span>
    </ui-select-choices>
</ui-select>
Run Code Online (Sandbox Code Playgroud)

javascript jquery tagging angularjs ui-select

6
推荐指数
1
解决办法
330
查看次数

更改 AWS ec2 实例的主机名

我有一个 AWS EC2 实例,其域类似于http://ec2-some-ip-addres.compute-1.amazonaws.com/

现在我想将这个 url(添加一个未注册的自定义域)更改为一些小的别名,如 testing.webserver.com 等。我们可以这样做吗?

我试过编辑 /etc/hosts 文件

ip-address hostname alias
Run Code Online (Sandbox Code Playgroud)

但不工作。更改 url 而不在 no-ip 或其他一些服务上创建 DNS 条目的最佳方法是什么?

hostname amazon-ec2 aliases amazon-web-services

5
推荐指数
1
解决办法
3086
查看次数

Django 模板:如何传递格式化字符串以包含标签

我是 Django 和 Django 模板的新手,我知道 Django 模板与 Rails 相比有一定的限制性。

所以我在电子邮件中包含一个模板,例如:

{% include "./partials/_info_row.html" with value=f'{request_count} requests' %}
Run Code Online (Sandbox Code Playgroud)

但这会引发错误:TemplateSyntaxError: Could not parse the remainder: ''{request_count} requests'' from 'f'{request_count} requests''

有没有办法传递这样的格式化字符串来包含标签?

django django-templates

5
推荐指数
1
解决办法
3611
查看次数

无法在 Mac OS X 上卸载 nginx

的输出nginx -vnginx version: nginx/1.14.0

运行brew uninstall nginxor 后brew remove nginx,报错:

Error: No such keg: /usr/local/Cellar/nginx
Run Code Online (Sandbox Code Playgroud)

我试过了 :

rm -f /usr/local/sbin/nginx
rm -f -R /usr/local/etc/nginx
rm -r /usr/local/opt/nginx
Run Code Online (Sandbox Code Playgroud)

但仍然nginx -v给出输出:nginx version: nginx/1.14.0

如何删除nginx安装?

nginx uninstallation

2
推荐指数
1
解决办法
1万
查看次数

React + TypeScript:将 React 组件作为道具传递,使用道具渲染 [错误:TS2339]

我正在使用 React + TypeScript。我有一个场景,我必须将一个React.SFC组件传递给另一个组件。我正在这样做:

容器.tsx

import ChildComp from './ChildComp';
<ParentComponent CustomComp={ ChildComp } someArray={ [1, 2] } />
Run Code Online (Sandbox Code Playgroud)

现在的问题是,我想ChildCompsomeArray值迭代这个组件, inside ParentComponent

这个子组件有它自己的 prop someValue,我已经向它添加了类型,比如这个子组件可以接受的类型,并且我在 Parent 内部迭代它时传递了这个 prop。

父组件.tsx

const content = someArray.map((value, index) => (
  <CustomComp someValue={ value } key={ index } />
));
{ content }
Run Code Online (Sandbox Code Playgroud)

但我收到错误,那 TS2339: Property 'someValue' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }'.

虽然,如果我在Container.tsx导入它的地方直接使用上面的迭代,它工作正常。但如果我将它传递给另一个组件,则不起作用。我在这里错过了什么吗?

注意:由于相同的迭代正在处理Container.tsx,我将迭代的内容传递给ParentComponent并像变量一样呈现它:

{ …
Run Code Online (Sandbox Code Playgroud)

javascript typescript reactjs react-props react-component

2
推荐指数
1
解决办法
4597
查看次数