【发布时间】:2019-02-16 18:47:12
【问题描述】:
我目前正在使用 React-router-dom(React 路由器 4),我想要一个客户的详细信息页面。我可以成功获取所有数据,但数据显示在与列表/概览相同的页面上。图片会澄清一些事情。我想要两个不同的视图。
这是路由器的结构:
const unauthenticatedPages = [
'/',
'/login'
];
const authenticatedPages = [
'/admin'
];
// public pages
const publicPage = () => {
if(Meteor.userId()) {
history.push('/');
}
};
// private pages
const privatePage = () => {
if(Meteor.userId()) {
history.push('/');
}
};
// check authentication for pages
export const onAuthenticationChange = (authenticated) => {
console.log('is authenticated...', authenticated);
const path = this.location.pathname;
const isUnauthenticatedPage = unauthenticatedPages.includes(path);
const isAuthenticatedPage = authenticatedPages.includes(path);
if( authenticated && isUnauthenticatedPage ) {
console.log('Can view the page routed to the path dashboard');
} else if (!authenticated && isAuthenticatedPage) {
console.log('No rights to view the page... routed to the path
login page');
}
}
// render app inside custom templates
const RouteWithLayout = ({layout, component, ...rest}) => {
return (
<Route {...rest} render={ (props) => React.createElement( layout, props, React.createElement(component, props)) } />
);
};
export const routes = (
<BrowserRouter>
<Switch>
<RouteWithLayout exact path="/" layout={AuthenticationLayout} onEnter={publicPage} component={AuthLogin} />
<RouteWithLayout path="/login" layout={AuthenticationLayout} onEnter={publicPage} component={AuthLogin} />
<AdminLayout>
<Route path="/admin" component={AdminDashboard} />
<Route path="/klanten/:customerID" component= . {CustomerDetails} />
<Route path="/klanten" component={CustomerOverview} />
<Route path="/nieuwe-klant" component={CustomerCreate} />
</AdminLayout>
<Route path="*" component={PageNotFound} />
</Switch>
</BrowserRouter>
);
额外信息: 我对这个路由器还有一些问题。我使用了 alanning:roles,因为用户必须根据他们所拥有的角色成为路由器。该图像显示了管理区域,但用户将使用具有较少选项的模拟布局。我怎样才能做到这一点?
如果我使用像 /new-customer/ 这样的单独路由,我会看到一个只有一个表单的新页面,但我想使用 /customer/new/ 作为路由。如果我将路由更改为 /customer/new/ 我的页面将不会被呈现。
/new-customer/ 页面的代码:
class CustomerCreate extends Component {
constructor(props) {
super(props);
this.state = {
message: ''
};
}
onFormSubmit(event) {
event.preventDefault();
const relationNumber = this.refs.relationNumber.value;
const companyName = this.refs.companyName.value;
const locationStreet = this.refs.locationStreet.value;
const locationPostal = this.refs.locationPostal.value;
const locationCity = this.refs.locationCity.value;
const contactPersonName = this.refs.contactPersonName.value;
const contactPersonEmail = this.refs.contactPersonEmail.value;
const contactPersonPhone = this.refs.contactPersonPhone.value;
const password = this.refs.password.value;
const confirmPassword = this.refs.confirmPassword.value;
const checkEmail = (email) => {
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const validated = regex.test(email);
return validated;
};
const checkPassword = ( pwd, pwd2 ) => {
if( pwd != '' || pwd2 != '' ){
if ( pwd.length >= 5 ) {
if ( pwd === pwd2 ) {
return true;
} else {
return false;
}
} else {
return false;
}
}
};
const validatedEmail = checkEmail( contactPersonEmail );
const validatedPassword = checkPassword( password, confirmPassword
);
if(!validatedEmail) {
this.setState({message: 'Het e-mailadres is niet correct.'});
}
if(!validatedPassword ) {
this.setState({message: 'De wachtwoorden komen niet overeen!'});
}
if( validatedEmail && validatedPassword ) {
try {
const createdAccount = Accounts.createUser({
email : contactPersonEmail,
password : password,
relationNumber : relationNumber,
companyName : companyName ,
locationStreet : locationStreet,
locationPostal : locationPostal,
locationCity : locationCity,
contactName : contactPersonName,
contactPhone : contactPersonPhone,
isArchived : 0,
setRoles : ['customer']
});
this.setState({message: 'De klant is succesvol toegevoegd!'});
// send flashmessage of succesfull state
return createdAccount;
} catch( err ) {
return err.reason;
}
}
return false;
}
render() {
return (
<div>
form sitting here ...
</div>
);
}
}
export default CustomerCreate;
【问题讨论】:
-
尝试将exact={true} 添加到:
因为我认为它现在正在加载这条路线和下一条路线,即 /klanten。
标签: javascript reactjs meteor react-router-v4