我稍微调整了React Router示例,让私有路由与Redux一起玩得很好,但在链接或重定向到其他"页面"时没有呈现任何组件.这个例子可以在这里找到:
https://reacttraining.com/react-router/web/example/auth-workflow
他们的PrivateRoute组件如下所示:
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
fakeAuth.isAuthenticated ? (
<Component {...props}/>
) : (
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}}/>
)
)}/>
)
Run Code Online (Sandbox Code Playgroud)
但是,因为我已将它合并到Redux应用程序中,所以我不得不稍微调整一下PrivateRoute,以便我可以访问redux存储以及路径Props:
const PrivateRouteComponent = (props) => (
<Route {...props.routeProps} render={() => (
props.logged_in ? (
<div>{props.children}</div>
) : (
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}} /> )
)} />
);
const mapStateToProps = (state, ownProps) => …Run Code Online (Sandbox Code Playgroud) 我有一个数组,里面有几天.每天都是一个对象,例如:
{day_year: "2012", day_month: "08", day_number: "03", day_name: "mon"}
我还为每个日期对象添加了一个timestamp属性,方法是:
function convertDays() {
var max_i = days.length;
for(var i = 0; i < max_i; i++) {
var tar_i = days[i];
tar_i.timestamp = new Date(tar_i.day_year, tar_i.day_month, tar_i.day_number);
}
}
Run Code Online (Sandbox Code Playgroud)
数组中的日子是任意的,因此它们没有真正的逻辑.
现在我想找到任何给定日期的最近两天.所以如果带有days的数组包含
我搜索2012年8月11日,我希望它返回2012年8月4日和2012年8月23日.
我尝试过使用另一个问题的答案,如下所示:
function findClosest(a, x) {
var lo, hi;
for(var i = a.length; i--;) {
if(a[i] <= x && (lo === undefined || lo < a[i])) lo = a[i];
if(a[i] >= x && (hi === undefined || …Run Code Online (Sandbox Code Playgroud) 我有一堆HTML数据,我正在使用PHP写入PDF文件.在PDF中,我希望剥离和清理所有HTML.例如:
<ul>
<li>First list item</li>
<li>Second list item which is quite a bit longer</li>
<li>List item with apostrophe 's 's</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
应该成为:
First list item
Second list item which is quite a bit longer
List item with apostrophe 's 's
Run Code Online (Sandbox Code Playgroud)
但是,如果我只是使用strip_tags(),我得到这样的东西:
First list item

Second list item which is quite a bit
longer

List item with apostrophe ’s ’s
Run Code Online (Sandbox Code Playgroud)
还要注意输出的缩进.
有关如何正确清理HTML以获得漂亮,干净的字符串而没有杂乱的空格和奇怪字符的任何提示?
谢谢 :)
我有10,000张图像,我想按颜色排序以进行打印.
我已经走得很远了.我已经平均了他们的颜色,所以现在我有两个目录:一个包含所有原始图像(original_images /),另一个包含平均颜色(平均值/)的同名jpeg.
接下来,我使用PHP对平均图像进行排序:
// $images is an array with all the filenames.
$sorted_images = array();
$loop_limit = count($images);
for($i = 0; $i < $loop_limit; $i++) {
$image = imagecreatefromjpeg("averages/" . $images[$i]);
$rgb = imagecolorat($image, 50, 50);
imagedestroy($image);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$hsv = rgb_to_hsv($r, $g, $b); // function to convert rgb to Hue/Sat/Value
$h = (string) $hsv['H'];
if(isset($sorted_h[$h])) {
$duplicates++;
echo("oh no! " . …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用JavaScript循环一个对象,并将该对象的所有子对象添加到HTML5画布中.
画布位正在工作,没有问题,但由于某种原因,我的所有图像最终都是相同的大小 - 最后一个子对象'背景'的大小.我假设它与我的循环范围和'this'有关,但我真的不知道要改变什么;
var stage;
var items = {
head: {image: null, path: "images/avatar-elements/head01.png", w:200, h:200},
hair: {image: null, path: "images/avatar-elements/hair01.png", w:200, h:200},
nose: {image: null, path: "images/avatar-elements/nose01.png", w:200, h:200},
eyes: {image: null, path: "images/avatar-elements/eyes01.png", w:200, h:200},
eyebrows: {image: null, path: "images/avatar-elements/eyebrows01.png", w:200, h:200},
ears: {image: null, path: "images/avatar-elements/ears01.png", w:200, h:200},
background: {image: null, path: "images/avatar-elements/background01.png", w:500, h:370}
};
function initCanvas() {
stage = new Kinetic.Stage("canvas", 500, 370);
makeBasis();
}
function makeBasis() {
for(i in items) {
var img …Run Code Online (Sandbox Code Playgroud) 我使用以下命令将HTML5文件中的JPEG文件作为字符串发布到PHP:
function createJPG() {
var dataUrl = document.getElementById('canvas').childNodes[4].toDataURL("image/jpeg");
console.log(dataUrl);
var params = "theimage=" + dataUrl;
var http = new XMLHttpRequest();
http.open("POST", "/avatar/php/save-avatar.php", true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(params);
}
Run Code Online (Sandbox Code Playgroud)
一切都很好,dataUrl是正确的(在控制台中)并包含一个有效的JPEG文件.但是当我用PHP检索它时,所有的加号都被空格所取代?
PHP代码:
$name = "image.jpg";
$data=$_POST['theimage'];
file_put_contents($name, base64_decode(substr($data, strpos($data, ",")+1)));
file_put_contents("../images/avatars/uploads/generated" . rand(0,200) . ".txt", $data);
Run Code Online (Sandbox Code Playgroud)
我使用文本文件来查看数据的样子,因为我无法弄清楚如何回显到JS :).该文本文件包含与JS控制台日志完全相同的数据,但没有+'es和替换空格,这导致JPEG文件损坏.
我该怎么办?
javascript ×3
php ×3
arrays ×1
canvas ×1
html ×1
html5-canvas ×1
image ×1
loops ×1
react-redux ×1
react-router ×1
reactjs ×1
redux ×1
scope ×1
search ×1
sorting ×1
strip-tags ×1