小编ind*_*dex的帖子

在同构的 reactjs 中检测移动设备

我应该如何在我的 reactjs + express 中实现检测移动设备?我使用mobile-detect来确定是否移动,但首先我实现了它,const md = new MobileDetect(window.navigator.userAgent)但我记得window当它加载服务器时不存在。express 部分中的示例应该可以工作,因为这是我唯一可以访问的地方,req但是我如何将它传递到我的 React 应用程序中以供之后使用?

更新

// app.js

...
import { routes } from './routes';
import { match, RouterContext } from 'react-router';
import { renderToString } from 'react-dom/server';

...
const app = express();
app.use(express.static('public'));

app.set('view engine', 'ejs');

app.get('*', (req, res) => {
  // routes is our object of React routes defined above
  match({ routes, location: req.url }, (err, redirectLocation, props) => {
  if (err) { …
Run Code Online (Sandbox Code Playgroud)

mobile express reactjs isomorphic-javascript

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

同构 reactjs cdn 资产

所以我几乎完成了我的第一个(同构)ReactJS,当我们部署它时,完成起来有点慢build.js。一个建议是使用 CDN 来分离资产获取(cdn0、cdn1、cdn2...),我想知道如何在我的站点中做到这一点。在本地我的结构是

- build/
- config/
  - webpack-development.config.js
  - webpack-production.config.js
- node_modules/
- package.json
- public/
  - assets/ // (this is where my assets are)
    - css/
    - img/
  - build.js
- README.md
- src/
- views/
Run Code Online (Sandbox Code Playgroud)

index.ejs现在就是这样

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <base href="/" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <link rel="shortcut icon" type="image/x-icon" href="assets/img/ico-a-brain.png" />
    <link rel="stylesheet" href="assets/css/normalize.css">
    <link rel="stylesheet" …
Run Code Online (Sandbox Code Playgroud)

javascript cdn reactjs webpack

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

怎么办"redirect_to并返回"完全退出或立即redirect_to?

我有这个作为我的代码:

def index
  @monkeys = Monkey.where(owner: current_user)
end

def new
  @monkey = Monkey.new name: 'Monkey 1', alive: true, owner: current_user
end

def create
  @monkey = Monkey.create monkey_params
  # Other fields and save here
end

def edit
  @monkey = Monkey.find_by(id: params[:id], owner: current_user)
  check_on_monkey_first(@monkey)
end

def update
  @monkey = Monkey.find_by(id: params[:id], owner: current_user)
  check_on_monkey_first(@monkey)

  if @monkey.update(monkey_params)
    redirect_to monkeys_path, success: 'Monkey saved'
  end
end

private
def check_on_monkey_first(monkey)
  redirect_to monkeys_path, flash: {info: "Monkey doesn't exist"} and return unless monkey
  redirect_to monkeys_path, flash: {info: …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails

3
推荐指数
1
解决办法
1842
查看次数

如何通过 Rails 中的控制器或视图的路由添加额外的参数

我知道在轨道路线中我可以做一些类似在控制器或视图中可用的事情get 'books/*section/:title', to: 'books#show'params[:title]

但我找不到一些东西来获得可用的固定额外参数。就像,我总是会得到类似params[:preview] = true总是基于路线的某些参数的东西。

我知道我总是可以做这样的事情:

def new
  params.merge!({preview: true})
end
Run Code Online (Sandbox Code Playgroud)

但我想知道是否还有其他方法。

parameters routes ruby-on-rails

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